-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinaryTreePaths.java
More file actions
43 lines (41 loc) · 1.05 KB
/
BinaryTreePaths.java
File metadata and controls
43 lines (41 loc) · 1.05 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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class BinaryTreePaths {
/*O(n^2) time as for each path it needs to copy each characters in the path.
Worst case is like:
1
/ \
2 3
/ \
4 5
/ \
6 7
*/
private static void dfs(TreeNode cur, StringBuilder sb, List<String> result) {
if (cur == null) {
return;
}
int sz = sb.length();
sb.append(cur.val);
if (cur.left == null && cur.right == null) {
result.add(sb.toString());
} else {
sb.append("->");
dfs(cur.left, sb, result);
dfs(cur.right, sb, result);
}
sb.setLength(sz);
}
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
dfs(root, new StringBuilder(), result);
return result;
}
}