forked from cherryljr/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary Tree Path Sum.java
More file actions
81 lines (71 loc) · 1.9 KB
/
Binary Tree Path Sum.java
File metadata and controls
81 lines (71 loc) · 1.9 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
典型的 深度优先搜索(DFS) 题目
参考 Combination Sum II 分析即可。
同时,若不知道如何书写 / 分析 DFS 程序可以参考 Subset.java
/*
Description
Given a binary tree, find all paths that sum of the nodes in the path equals to a given number target.
A valid path is from root node to any of the leaf nodes.
Example
Given a binary tree, and target = 5:
1
/ \
2 4
/ \
2 3
return
[
[1, 2, 2],
[1, 4]
]
Tags
Binary Tree Binary Tree Traversal
*/
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root the root of binary tree
* @param target an integer
* @return all valid paths
*/
public List<List<Integer>> binaryTreePathSum(TreeNode root, int target) {
List<List<Integer>> rst = new ArrayList<>();
if (root == null) {
return rst;
}
dfs(rst, new ArrayList<Integer>(), root, target);
return rst;
}
private void dfs(List<List<Integer>> rst,
ArrayList<Integer> list,
TreeNode t,
int remainTarget) {
if (t == null) {
return;
}
list.add(t.val);
if (t.left == null && t.right == null && t.val == remainTarget) {
rst.add(new ArrayList<Integer>(list));
return;
}
if (t.left != null) {
dfs(rst, list, t.left, remainTarget - t.val);
// Backtracking
list.remove(list.size() - 1);
}
if (t.right != null) {
dfs(rst, list, t.right, remainTarget - t.val);
// Backtracking
list.remove(list.size() - 1);
}
}
}