-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSum2.java
More file actions
63 lines (58 loc) · 1.46 KB
/
PathSum2.java
File metadata and controls
63 lines (58 loc) · 1.46 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.ArrayList;
import java.util.Collections;
import java.util.Stack;
public class PathSum2 {
Stack<TreeNode> stack = new Stack<TreeNode>();
int con;
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
con = sum;
if (root == null) {
return result;
}
stack.add(root);
preorder(root, 0);
stack.pop();
return result;
}
//pre-order traversal, actually DFS
public void preorder(TreeNode root, int prev) {
if (root.left == null && root.right == null) {
if (prev + root.val == con) {
ArrayList<Integer> arr = new ArrayList<Integer>();
Stack<TreeNode> c = (Stack<TreeNode>) stack.clone();
while (!c.isEmpty()) {
arr.add(c.pop().val);
}
Collections.reverse(arr);
result.add(arr);
return;
}
else {
return;
}
}
if (root.left != null) {
stack.add(root.left);
preorder(root.left, prev + root.val);
stack.pop();
}
if (root.right != null) {
stack.add(root.right);
preorder(root.right, prev + root.val);
stack.pop();
}
}
public void testSum() {
TreeNode root = new TreeNode(5);
root.left = new TreeNode(3);
root.right = new TreeNode(2);
ArrayList<ArrayList<Integer>> result = pathSum(root, 8);
for (ArrayList<Integer> arr: result) {
for (Integer x: arr) {
System.out.print(x + " ");
}
System.out.print("\n");
}
}
}