-
Notifications
You must be signed in to change notification settings - Fork 43
/
PathSum.java
84 lines (65 loc) · 2.18 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package LeetCodeJava.DFS;
// https://leetcode.com/problems/path-sum/
import LeetCodeJava.DataStructure.TreeNode;
import java.util.LinkedList;
public class PathSum {
// V0
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null){
return false;
}
if (root.left == null && root.right == null){
return root.val == targetSum;
}
return checkSum(root, targetSum);
}
private Boolean checkSum(TreeNode root, int targetSum){
if (root == null){
return false;
}
targetSum -= root.val;
if (root.left == null && root.right == null){
return targetSum == 0;
}
return checkSum(root.left ,targetSum) || checkSum(root.right ,targetSum);
}
// V1
// https://leetcode.com/problems/path-sum/editorial/
// IDEA : RECURSION
public boolean hasPathSum_2(TreeNode root, int sum) {
if (root == null)
return false;
sum -= root.val;
if ((root.left == null) && (root.right == null))
return (sum == 0);
return hasPathSum_2(root.left, sum) || hasPathSum_2(root.right, sum);
}
// V2
// https://leetcode.com/problems/path-sum/editorial/
// IDEA : Iterations
public boolean hasPathSum_3(TreeNode root, int sum) {
if (root == null)
return false;
LinkedList<TreeNode> node_stack = new LinkedList();
LinkedList<Integer> sum_stack = new LinkedList();
node_stack.add(root);
sum_stack.add(sum - root.val);
TreeNode node;
int curr_sum;
while ( !node_stack.isEmpty() ) {
node = node_stack.pollLast();
curr_sum = sum_stack.pollLast();
if ((node.right == null) && (node.left == null) && (curr_sum == 0))
return true;
if (node.right != null) {
node_stack.add(node.right);
sum_stack.add(curr_sum - node.right.val);
}
if (node.left != null) {
node_stack.add(node.left);
sum_stack.add(curr_sum - node.left.val);
}
}
return false;
}
}