-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreePaths.java
More file actions
75 lines (60 loc) · 1.59 KB
/
BinaryTreePaths.java
File metadata and controls
75 lines (60 loc) · 1.59 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 lintcode;
import java.util.ArrayList;
import java.util.List;
/**
* Created by caixin on 10/12/17.
*
* binary tree
* 1
* / \
* 2 6
* /
* 3
* /
* 4
* /
* 5
*
*
* output : [1->2->3]
*/
public class BinaryTreePaths {
public static void main(String[] args) {
TreeNode node1 = new TreeNode(1);
TreeNode node2 = new TreeNode(2);
TreeNode node3 = new TreeNode(3);
TreeNode node4 = new TreeNode(4);
TreeNode node5 = new TreeNode(5);
TreeNode node6 = new TreeNode(6);
node1.left = node2;
node2.left = node3;
node3.left = node4;
node4.left = node5;
node1.right = node6;
BinaryTreePaths binaryTreePaths = new BinaryTreePaths();
System.out.println(binaryTreePaths.solution(node1));
}
public List<String> solution(TreeNode root){
List<String> result = new ArrayList<String>();
if (root == null){
return result;
}
helper(result,root,"");
return result;
}
public void helper(List<String> result, TreeNode root, String path){
//leaf node
if (root.right == null && root.left == null){
result.add(path + root.val);
}
// not leaf node, 左节点
if (root.left != null){
//递归调用
helper(result,root.left, path+root.val+ "->");
}
// not leaf node , 右节点
if (root.right!=null){
helper(result,root.right,path+root.val+"->");
}
}
}