-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSum.java
More file actions
41 lines (40 loc) · 1.35 KB
/
Copy pathPathSum.java
File metadata and controls
41 lines (40 loc) · 1.35 KB
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
32
33
34
35
36
37
38
39
40
41
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
// time : O(n);
// space : O(n);
class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false;// to detect root is null or giving an option for later excuting if either one of root.left and root.right is null.
if(root.left == null && root.right == null) return sum == root.val;
//checking when u hitting each path to the end.
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); // excuting the one is true;
}
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null) return false;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode cur = root;
cur = stack.pop();
if(cur.left == null && cur.right == null){
if(sum == cur.val) return true;
}
if(cur.left != null) {
stack.push(cur.left);
cur.left.val += cur.val;
}
if(cur.right != null) {
stack.push(cur.right);
cur.right.val += cur.val;
}
}
return false;
}
}