-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreePath.java
More file actions
64 lines (52 loc) · 1.5 KB
/
BinaryTreePath.java
File metadata and controls
64 lines (52 loc) · 1.5 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
package BackTracking;
import java.util.ArrayList;
import java.util.List;
public class BinaryTreePath {
// TreeNode definition
static class TreeNode {
int val;
TreeNode left, right;
TreeNode(int val) { this.val = val; }
}
public static void main(String[] args) {
// Build sample tree:
// 1
// / \
// 2 3
// \
// 5
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.right = new TreeNode(5);
BinaryTreePath sol = new BinaryTreePath();
List<String> paths = sol.binaryTreePaths(root);
System.out.println("Root-to-leaf paths:");
for (String path : paths) {
System.out.println(path);
}
}
public List<String> binaryTreePaths(TreeNode root) {
List<String> sList = new ArrayList<>();
backTrack(root,"",sList);
return sList;
}
static void backTrack(TreeNode root, String str,List<String> sList) {
if(root == null) return;
if(str.length()==0){
str = root.val+"";
} else {
str = str+"->"+root.val+"";
}
if(root.left == null && root.right == null) {
sList.add(str);
return;
}
backTrack(root.left,str,sList);
backTrack(root.right,str,sList);
return;
}
}
// Root-to-leaf paths:
// 1->2->5
// 1->3