1
1
# Binary Tree Postorder Traversal
2
2
3
+ Tags: Tree, Stack, Hard
4
+
3
5
## Question
4
6
5
- - leetcode: [ Binary Tree Postorder Traversal | LeetCode OJ ] ( https://leetcode.com/problems/binary-tree-postorder-traversal/ )
6
- - lintcode: [ (68) Binary Tree Postorder Traversal] ( http://www.lintcode.com/en/problem/binary-tree-postorder-traversal/ )
7
+ - leetcode: [ Binary Tree Postorder Traversal] ( https://leetcode.com/problems/binary-tree-postorder-traversal/ )
8
+ - lintcode: [ Binary Tree Postorder Traversal] ( http://www.lintcode.com/en/problem/binary-tree-postorder-traversal/ )
7
9
8
10
### Problem Statement
9
11
10
12
Given a binary tree, return the _ postorder_ traversal of its nodes' values.
11
13
12
- #### Example
13
-
14
- Given binary tree ` {1,#,2,3} ` ,
15
-
16
-
14
+ For example:
15
+ Given binary tree ` {1,#,2,3} ` ,
17
16
17
+
18
+
19
+
18
20
1
19
21
\
20
22
2
21
23
/
22
24
3
23
-
25
+
24
26
25
27
return ` [3,2,1] ` .
26
28
27
- #### Challenge
28
-
29
- Can you do it without recursion?
29
+ ** Note:** Recursive solution is trivial, could you do it iteratively?
30
30
31
31
32
32
## 题解1 - 递归
@@ -124,6 +124,32 @@ public class Solution {
124
124
}
125
125
```
126
126
127
+ ### Java - Traversal
128
+
129
+ ``` java
130
+ /**
131
+ * Definition for a binary tree node.
132
+ * public class TreeNode {
133
+ * int val;
134
+ * TreeNode left;
135
+ * TreeNode right;
136
+ * TreeNode(int x) { val = x; }
137
+ * }
138
+ */
139
+ public class Solution {
140
+ private List<Integer > result = new ArrayList<Integer > ();
141
+
142
+ public List<Integer > postorderTraversal (TreeNode root ) {
143
+ if (root != null ) {
144
+ postorderTraversal(root. left);
145
+ postorderTraversal(root. right);
146
+ result. add(root. val);
147
+ }
148
+ return result;
149
+ }
150
+ }
151
+ ```
152
+
127
153
### 源码分析
128
154
129
155
递归版的太简单了,没啥好说的,注意入栈顺序。
@@ -238,10 +264,9 @@ public:
238
264
public class Solution {
239
265
public List<Integer > postorderTraversal (TreeNode root ) {
240
266
List<Integer > result = new ArrayList<Integer > ();
241
- if (root == null ) return result;
242
-
243
267
Deque<TreeNode > stack = new ArrayDeque<TreeNode > ();
244
- stack. push(root);
268
+
269
+ if (root != null ) stack. push(root);
245
270
TreeNode prev = null ;
246
271
while (! stack. isEmpty()) {
247
272
TreeNode curr = stack. peek();
@@ -250,10 +275,9 @@ public class Solution {
250
275
if (prev != null && (curr. left == prev || curr. right == prev)) {
251
276
childVisited = true ;
252
277
}
253
-
254
278
if (noChild || childVisited) {
279
+ curr = stack. pop();
255
280
result. add(curr. val);
256
- stack. pop();
257
281
prev = curr;
258
282
} else {
259
283
if (curr. right != null ) stack. push(curr. right);
@@ -268,7 +292,7 @@ public class Solution {
268
292
269
293
### 源码分析
270
294
271
- 遍历顺序为『左右根』,判断根节点是否应该从栈中剔除有两种条件,一为无子节点,二为子节点已遍历过。判断子节点是否遍历过需要排除` prev == null ` 的情况,因为 prev 初始化为 null.
295
+ 遍历顺序为『左右根』,判断根节点是否应该从栈中剔除有两种条件,一为无子节点,二为子节点已遍历过。判断子节点是否遍历过需要排除` prev == null ` 的情况,因为 prev 初始化为 null. Java 中在 while 内部新建 curr 变量而不是复用 root 有一定性能提升,不知是不是 JVM 运行时优化导致的。
272
296
273
297
** 将递归写成迭代的难点在于如何在迭代中体现递归本质及边界条件的确立,可使用简单示例和纸上画出栈调用图辅助分析。**
274
298
@@ -326,35 +350,28 @@ public:
326
350
327
351
```java
328
352
/**
329
- * Definition of TreeNode:
353
+ * Definition for a binary tree node.
330
354
* public class TreeNode {
331
- * public int val;
332
- * public TreeNode left, right;
333
- * public TreeNode(int val) {
334
- * this.val = val;
335
- * this.left = this.right = null;
336
- * }
355
+ * int val;
356
+ * TreeNode left;
357
+ * TreeNode right;
358
+ * TreeNode(int x) { val = x; }
337
359
* }
338
360
*/
339
361
public class Solution {
340
- /**
341
- * @param root: The root of binary tree.
342
- * @return: Postorder in ArrayList which contains node values.
343
- */
344
- public ArrayList<Integer> postorderTraversal(TreeNode root) {
345
- ArrayList<Integer> result = new ArrayList<Integer>();
346
- if (root == null) return result;
347
-
362
+ public List<Integer> postorderTraversal(TreeNode root) {
363
+ List<Integer> result = new ArrayList<Integer>();
348
364
Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
349
- stack.push(root);
365
+ if (root != null) stack.push(root);
366
+
350
367
while (!stack.isEmpty()) {
351
- TreeNode node = stack.pop();
352
- result.add(node .val);
353
- if (node .left != null) stack.push(node .left);
354
- if (node .right != null) stack.push(node .right);
368
+ TreeNode curr = stack.pop();
369
+ result.add(curr .val);
370
+ if (curr .left != null) stack.push(curr .left);
371
+ if (curr .right != null) stack.push(curr .right);
355
372
}
356
- Collections.reverse(result);
357
373
374
+ Collections.reverse(result);
358
375
return result;
359
376
}
360
377
}
0 commit comments