-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathBinaryTreePath.java
59 lines (49 loc) · 1.38 KB
/
BinaryTreePath.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
package algorithm;
import java.util.ArrayList;
import java.util.List;
/**
* 寻找二叉树根节点到数上任意节点的路径
*/
public class BinaryTreePath {
static class TreeNode {
TreeNode left;
TreeNode right;
int val;
public TreeNode(int value) {
this.val = value;
}
}
private static boolean getPathFromRoot(TreeNode root, TreeNode target, List<Integer> list) {
if (root == null || target == null) {
return false;
}
list.add(root.val);
if (root.val == target.val) {
return true;
}
if (getPathFromRoot(root.left, target, list)){
return true;
}
if (getPathFromRoot(root.right, target, list)){
return true;
}
list.remove((Integer) root.val);
return false;
}
public static void main(String[] args) {
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
n1.left = n2;
n1.right = n3;
n2.left = n4;
n2.right = n5;
ArrayList<Integer> list = new ArrayList<>();
getPathFromRoot(n1, n5, list);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}