-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy path257. Binary Tree Paths.java
executable file
·159 lines (130 loc) · 3.8 KB
/
257. 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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
E
tags: Binary Tree, DFS, Backtracking
time: O(n)
space: O(nlogn)
给一个binary tree, 返回所有root-to-leaf path
#### DFS, backtracking
- Find all paths, bfs/dfs all works. dfs will be simplier to write
- Recursive:分叉. dfs.
- top->bottom: enumerate current node into the list, carry to next level, and backtrack
- top->bottom is trivial to consider: path flows from top->bottom
- time: visit all n nodes
- space: to hold all paths, O(nlogn)
- O((n-1)/2) = O(n) nodes at leaf
- O(logn) depth
#### DFS, bottom->up
- We can also take current node.left or node.right to generate list of results from the subproblem
- let dfs return list of string candidates, and we can run pair the list with currenet node, once they come back.
- TODO: can write code to practice
#### Iterative
- Iterative, 非递归练习了一下
- 因为要每次切短list, 所以再加了一个Stack 来存level
- 单这道题用dfs更简单, 因为找的就是从头到尾的path, 是dfs的pattern
```
/*
Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
Example
Given the following binary tree:
1
/ \
2 3
\
5
All root-to-leaf paths are:
[
"1->2->5",
"1->3"
]
Tags Expand
Binary Tree Binary Tree Traversal Facebook Google
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> rst = new ArrayList<>();
dfs(rst, new ArrayList<>(), root);
return rst;
}
public void dfs(List<String> rst, List<String> path, TreeNode node) {
if (node == null) return;
path.add(String.valueOf(node.val));
if (node.left == null && node.right == null) { // leaf
rst.add(convert(path));
path.remove(path.size() - 1);
return;
}
dfs(rst, path, node.left);
dfs(rst, path, node.right);
path.remove(path.size() - 1);
}
private String convert(List<String> path) {
return String.join("->", path);
}
}
/*
Previous notes
Thoughts:
Recursive, need a helper (root, arraylist<Integer> list, List<String>)
Add curr.
if(root.left == null && root.right == null) {
convert to String, add to list.
}
Go left. remove last node.
Go right, remove last node.
time: 2715m
*/
/*
Iterative:
Use a stack. Note. Process push curr, right, left
Special: use a levels stack, to track level. Because we are not backtracking in interative mode,
so we need manually adjust list's length back to be same level as next item in stack.
time: 2715m
*/
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> rst = new ArrayList<String>();
if (root == null) {
return rst;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
ArrayList<Integer> list = new ArrayList<Integer>();
stack.push(root);
Stack<Integer> levels = new Stack<Integer>();
levels.push(0);
while (!stack.isEmpty()) {
int lv = levels.pop();
TreeNode node = stack.pop();
list.add(node.val);
if (node.left == null && node.right == null) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < list.size() - 1; i++) {
sb.append(list.get(i) + "->");
}
sb.append(list.get(list.size() - 1));
rst.add(sb.toString());
while (!levels.isEmpty() && list.size() > levels.peek()) {
list.remove(list.size() - 1);
}
}
if (node.right != null) {
stack.push(node.right);
levels.push(lv + 1);
}
if (node.left != null) {
stack.push(node.left);
levels.push(lv + 1);
}
}//end while
return rst;
}
}
```