Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
200 lines (170 loc) · 4.97 KB

File metadata and controls

executable file
·
200 lines (170 loc) · 4.97 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
E
1519797926
tags: Binary Tree, DFS, Backtracking
给一个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
#### 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
*/
// cleaner:
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> rst = new ArrayList<>();
if (root == null) {
return rst;
}
dfs(rst, new ArrayList<>(), root);
return rst;
}
public void dfs(List<String> rst, List<Integer> list, TreeNode node) {
if (node == null) return;
list.add(node.val);
if (node.left == null && node.right == null) {
rst.add(convert(list));
list.remove(list.size() - 1);
return;
}
dfs(rst, list, node.left);
dfs(rst, list, node.right);
list.remove(list.size() - 1);
}
private String convert(List<Integer> list) {
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));
return sb.toString();
}
}
/*
Basic dfs, pass along sb, List<String>.
Save when root == null.
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> rst = new ArrayList<>();
if (root == null) {
return rst;
}
dfs(rst, new ArrayList<>(), root);
return rst;
}
public void dfs(List<String> rst, List<Integer> list, TreeNode node) {
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());
return;
}
if (node.left != null) {
dfs(rst, list, node.left);
list.remove(list.size() - 1);
}
if (node.right != null) {
dfs(rst, list, node.right);
list.remove(list.size() - 1);
}
}
}
/*
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;
}
}
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
```
Morty Proxy This is a proxified and sanitized view of the page, visit original site.