11# Binary Tree Postorder Traversal
22
3+ Tags: Tree, Stack, Hard
4+
35## Question
46
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/ )
79
810### Problem Statement
911
1012Given a binary tree, return the _ postorder_ traversal of its nodes' values.
1113
12- #### Example
13-
14- Given binary tree ` {1,#,2,3} ` ,
15-
16-
14+ For example:
15+ Given binary tree ` {1,#,2,3} ` ,
1716
17+
18+
19+
1820 1
1921 \
2022 2
2123 /
2224 3
23-
25+
2426
2527return ` [3,2,1] ` .
2628
27- #### Challenge
28-
29- Can you do it without recursion?
29+ ** Note:** Recursive solution is trivial, could you do it iteratively?
3030
3131
3232## 题解1 - 递归
@@ -124,6 +124,32 @@ public class Solution {
124124}
125125```
126126
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+
127153### 源码分析
128154
129155递归版的太简单了,没啥好说的,注意入栈顺序。
@@ -238,10 +264,9 @@ public:
238264public class Solution {
239265 public List<Integer > postorderTraversal (TreeNode root ) {
240266 List<Integer > result = new ArrayList<Integer > ();
241- if (root == null ) return result;
242-
243267 Deque<TreeNode > stack = new ArrayDeque<TreeNode > ();
244- stack. push(root);
268+
269+ if (root != null ) stack. push(root);
245270 TreeNode prev = null ;
246271 while (! stack. isEmpty()) {
247272 TreeNode curr = stack. peek();
@@ -250,10 +275,9 @@ public class Solution {
250275 if (prev != null && (curr. left == prev || curr. right == prev)) {
251276 childVisited = true ;
252277 }
253-
254278 if (noChild || childVisited) {
279+ curr = stack. pop();
255280 result. add(curr. val);
256- stack. pop();
257281 prev = curr;
258282 } else {
259283 if (curr. right != null ) stack. push(curr. right);
@@ -268,7 +292,7 @@ public class Solution {
268292
269293### 源码分析
270294
271- 遍历顺序为『左右根』,判断根节点是否应该从栈中剔除有两种条件,一为无子节点,二为子节点已遍历过。判断子节点是否遍历过需要排除` prev == null ` 的情况,因为 prev 初始化为 null.
295+ 遍历顺序为『左右根』,判断根节点是否应该从栈中剔除有两种条件,一为无子节点,二为子节点已遍历过。判断子节点是否遍历过需要排除` prev == null ` 的情况,因为 prev 初始化为 null. Java 中在 while 内部新建 curr 变量而不是复用 root 有一定性能提升,不知是不是 JVM 运行时优化导致的。
272296
273297** 将递归写成迭代的难点在于如何在迭代中体现递归本质及边界条件的确立,可使用简单示例和纸上画出栈调用图辅助分析。**
274298
@@ -326,35 +350,28 @@ public:
326350
327351```java
328352/**
329- * Definition of TreeNode:
353+ * Definition for a binary tree node.
330354 * 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; }
337359 * }
338360 */
339361public 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>();
348364 Deque<TreeNode> stack = new ArrayDeque<TreeNode>();
349- stack.push(root);
365+ if (root != null) stack.push(root);
366+
350367 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);
355372 }
356- Collections.reverse(result);
357373
374+ Collections.reverse(result);
358375 return result;
359376 }
360377}
0 commit comments