forked from walnutown/CodingInTheDeep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathSum2.java
More file actions
57 lines (51 loc) · 1.62 KB
/
Copy pathPathSum2.java
File metadata and controls
57 lines (51 loc) · 1.62 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
/*
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree and sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
// Backtracking
// The core algorithm of this question is easy, the difficulty lies in the termination condition of dfs
// Note that it should be a root ot leaf path with the given sum (sum may be negative)
// time: O(n); sapce: recursive stack
public class Solution {
public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if (root==null) return res;
dfs(root, res, new ArrayList<Integer>(), sum);
return res;
}
private void dfs(TreeNode node, ArrayList<ArrayList<Integer>> res, ArrayList<Integer> r, int sum){
if (node==null) return;
sum -= node.val;
r.add(node.val);
if (node.left==null && node.right==null){
if (sum==0) res.add(new ArrayList<Integer>(r));
r.remove(r.size()-1); // Remember to remove last node from the path before return
return;
}
dfs(node.left, res, r, sum);
dfs(node.right, res, r, sum);
r.remove(r.size()-1);
}
}