-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFindTreePath.java
More file actions
75 lines (66 loc) · 2.07 KB
/
Copy pathFindTreePath.java
File metadata and controls
75 lines (66 loc) · 2.07 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
package offer.problem34;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
* Created with IntelliJ IDEA
*
* @Author yuanhaoyue swithaoy@gmail.com
* @Description 34. 二叉树中和为某一值的路径
* @Date 2019-01-25
* @Time 21:09
*/
public class FindTreePath {
public ArrayList<ArrayList<Integer>> find(TreeNode root, int target) {
ArrayList<ArrayList<Integer>> list = new ArrayList<>();
if (root == null) {
return list;
}
Stack<Integer> stack = new Stack<>();
getPath(root, target, 0, list, stack);
return list;
}
private void getPath(TreeNode node, int target, int sum, ArrayList<ArrayList<Integer>> list,
Stack<Integer> stack) {
sum += node.val;
//及时剪枝
if (sum <= target) {
stack.push(node.val);
//满足条件
if (node.left == null && node.right == null && sum == target) {
list.add(new ArrayList<>(stack));
//出栈,回溯上一个点
stack.pop();
return;
}
//左子树
if (node.left != null) {
getPath(node.left, target, sum, list, stack);
}
//右子树
if (node.right != null) {
getPath(node.right, target, sum, list, stack);
}
//出栈,回溯上一个点
stack.pop();
}
}
public static class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public static void main(String[] args) {
TreeNode root = new TreeNode(10);
root.right = new TreeNode(5);
root.right.left = new TreeNode(4);
root.right.right = new TreeNode(7);
root.left = new TreeNode(12);
System.out.println("查找期望值为22的路径:");
List list = new FindTreePath().find(root, 22);
System.out.println(list);
}
}