forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path437_pathSum.java
31 lines (29 loc) · 1.04 KB
/
437_pathSum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//If the tree is balanced, then each node is reached from its ancestors (+ itself) only, which are up to log n. Thus, the time complexity for a balanced tree is O (n * log n).
//However, in the worst-case scenario where the binary tree has the same structure as a linked list, the time complexity is indeed O (n ^ 2).
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int pathSum(TreeNode root, int sum) {
if(root == null) return 0;
return dfs(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum); //Recursive for all notes with sum
}
public int dfs(TreeNode root, int sum){
int res = 0;
if(root == null) return res;
if(root.val == sum){
res++;
}
int left = dfs(root.left, sum-root.val);
int right = dfs(root.right, sum-root.val);
res += left;
res += right;
return res;
}
}