/
BinaryTreePaths.java
301 lines (269 loc) · 6.23 KB
/
BinaryTreePaths.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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package com.jacksen.java.leetcode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
/**
* 257. Binary Tree Paths <br />
*
* https://leetcode.com/problems/binary-tree-paths/
*
* @author jacksen
* @date 2016-5-20 下午4:00:28
*/
public class BinaryTreePaths {
public static void main(String[] args) {
TreeNode node1 = new TreeNode(1);
TreeNode node2 = new TreeNode(2);
TreeNode node3 = new TreeNode(3);
TreeNode node4 = new TreeNode(4);
TreeNode node5 = new TreeNode(5);
TreeNode node6 = new TreeNode(6);
node1.left = node2;
node1.right = node3;
node2.left = node4;
node3.left = node5;
node3.right = node6;
//
BinaryTreePaths bTreePaths = new BinaryTreePaths();
List<String> resultList = bTreePaths.binaryTreePaths4(node1);
for (int i = 0; i < resultList.size(); i++) {
System.out.println(resultList.get(i));
}
}
public List<String> resultList = new ArrayList<String>();
/**
* 递归方式
*
* <br />
*
* Run Time : 4ms
*
* @param root
* @return
*/
public List<String> binaryTreePaths(TreeNode root) {
if (root == null) {
return resultList;
}
List<String> singleResult = new ArrayList<>();
getTreePath(root, singleResult);
return resultList;
}
/**
* DFS
*
* @param resultList
* @param node
* @param singleResult
*/
private void getTreePath(TreeNode node, List<String> singleResult) {
singleResult.add(node.val + "");
if (node.left == null && node.right == null) {
resultList.add(getPath(singleResult));
}
if (node.left != null) {
getTreePath(node.left, new ArrayList<>(singleResult));
}
if (node.right != null) {
getTreePath(node.right, new ArrayList<>(singleResult));
}
}
/**
*
* @param singleResult
* @return
*/
private String getPath(List<String> singleResult) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < singleResult.size() - 1; i++) {
sb.append(singleResult.get(i) + "->");
}
sb.append(singleResult.get(singleResult.size() - 1));
return sb.toString();
}
/**
* 递归方法二 <br />
*
* https://leetcode.com/discuss/85025/sharing-my-recursive-java-solution
*
* @param root
* @return
*/
public List<String> binaryTreePaths2(TreeNode root) {
if (root == null) {
return resultList;
}
findPaths(root, root.val + "");
return resultList;
}
private void findPaths(TreeNode node, String path) {
if (node.left == null && node.right == null) {
resultList.add(path);
}
if (node.left != null) {
findPaths(node.left, path + "->" + node.left.val);
}
if (node.right != null) {
findPaths(node.right, path + "->" + node.right.val);
}
}
/**
* 递归方法三 <br />
*
* 在方法二的基础上修改,将String改成StringBuilder <br />
*
* https://leetcode.com/discuss/92435/java-2ms-solution-using-stringbuilder
*
* <br />
*
* Run Time : 2ms
*
* @param root
* @return
*/
public List<String> binaryTreePaths3(TreeNode root) {
findPath(root, new StringBuilder());
return resultList;
}
private void findPath(TreeNode node, StringBuilder sb) {
if (node == null) {
return;
}
int len = sb.length();
sb.append(node.val);
if (node.left == null && node.right == null) {
resultList.add(sb.toString());
} else {
sb.append("->");
findPath(node.left, sb);
findPath(node.right, sb);
}
sb.setLength(len);// 截取 !!!
}
/**
* 通过栈来做<br />
*
* https://leetcode.com/discuss/83013/my-java-non-recursion-solution-using-
* stack-and-wrapper
*
* <br />
*
* Run Time : 5ms
*
* @param root
* @return
*/
public List<String> binaryTreePaths4(TreeNode root) {
if (root == null) {
return resultList;
}
Stack<Wrapper> stack = new Stack<>();
stack.add(new Wrapper(root, root.val + ""));
Wrapper wrapper = null;
while (!stack.isEmpty()) {
wrapper = stack.pop();
if (wrapper.node.left == null && wrapper.node.right == null) {
resultList.add(wrapper.path);
}
if (wrapper.node.left != null) {
stack.add(new Wrapper(wrapper.node.left, wrapper.path + "->"
+ wrapper.node.left.val));
}
if (wrapper.node.right != null) {
stack.add(new Wrapper(wrapper.node.right, wrapper.path + "->"
+ wrapper.node.right.val));
}
}
return resultList;
}
/**
*
* @author Admin
*
*/
private static class Wrapper {
TreeNode node;
String path;
public Wrapper(TreeNode node, String path) {
this.node = node;
this.path = path;
}
}
/**
* BFS <br />
*
* with two Queue
*
* https://leetcode.com/discuss/67749/bfs-with-two-queue-java-solution
*
* @param root
* @return
*/
public List<String> binaryTreePaths5(TreeNode root) {
if (root == null) {
return resultList;
}
Queue<TreeNode> nodeQueue = new LinkedList<TreeNode>();
Queue<String> pathQueue = new LinkedList<>();
nodeQueue.offer(root);
pathQueue.offer(root.val + "");
while (!nodeQueue.isEmpty()) {
TreeNode currNode = nodeQueue.poll();
String item = pathQueue.poll();
if (currNode.left == null && currNode.right == null) {
resultList.add(item);
}
if (currNode.left != null) {
nodeQueue.offer(currNode.left);
pathQueue.offer(item + "->" + currNode.left.val);
}
if (currNode.right != null) {
nodeQueue.offer(currNode.right);
pathQueue.offer(item + "->" + currNode.right.val);
}
}
return resultList;
}
/**
* 不需要多创建一个函数 <br />
*
* 3ms
*
* https://leetcode.com/discuss/55451/clean-solution-accepted-without-helper
* -recursive-function
*
* @param root
* @return
*/
public List<String> binaryTreePaths6(TreeNode root) {
List<String> pathsList = new ArrayList<String>();
if (root == null) {
return pathsList;
}
if (root.left == null && root.right == null) {
pathsList.add(root.val + "");
return pathsList;
}
for (String path : binaryTreePaths6(root.left)) {
pathsList.add(root.val + "->" + path);
}
for (String path : binaryTreePaths6(root.right)) {
pathsList.add(root.val + "->" + path);
}
return pathsList;
}
/**
* @author jacksen
* @date 2016-5-20 下午4:01:14
*/
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
}