-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBinary_Tree_Paths.java
39 lines (39 loc) · 1.06 KB
/
Binary_Tree_Paths.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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<Integer> path = new ArrayList<Integer>();
List<String> paths = new ArrayList<String>();
if (root == null) {
return paths;
}
binaryTreePathsHelper(root, path, paths);
return paths;
}
public void binaryTreePathsHelper(TreeNode root, List<Integer> path, List<String> paths) {
if (root.left == null && root.right == null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < path.size(); ++i) {
sb.append(path.get(i));
sb.append("->");
}
sb.append(root.val);
paths.add(sb.toString());
}
path.add(root.val);
if (root.left != null) {
binaryTreePathsHelper(root.left, path, paths);
}
if (root.right != null) {
binaryTreePathsHelper(root.right, path, paths);
}
path.remove(path.size() - 1);
}
}