Skip to content

Commit 6feacb0

Browse files
committed
后序遍历二叉树
1 parent 82975ee commit 6feacb0

File tree

6 files changed

+149
-1
lines changed

6 files changed

+149
-1
lines changed

LeetCode/Doc/使用插入排序对链表进行排序.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# insertion-sort-list(使用插入排序对链表进行排序)
22

3-
<center>知识点:</center>
3+
<center>知识点:排序,链表</center>
44

55

66
## 题目描述

LeetCode/Doc/后序遍历二叉树.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# binary-tree-postorder-traversal(后序遍历二叉树)
2+
3+
<center>知识点:树</center>
4+
5+
6+
## 题目描述
7+
8+
Given a binary tree, return the postorder traversal of its nodes' values.
9+
10+
For example:
11+
Given binary tree{1,#,2,3},
12+
13+
![image-20190413113855000](https://ws2.sinaimg.cn/large/006tNc79gy1g20usuv6s0j308y0860so.jpg)
14+
15+
return[3,2,1].
16+
17+
**Note:** Recursive solution is trivial, could you do it iteratively?
18+
19+
给定一个二叉树,返回其后序遍历结果。
20+
21+
提示:递归解决方案是不值一提的,你选择迭代。
22+
23+
24+
## 解题思路
25+
26+
首先明确什么是后序遍历:对于二叉树访问顺序为左-右-根,即为后序遍历。
27+
28+
递归方式很简单,这里说一下迭代的思路,考虑如下二叉树:
29+
30+
![image-20190413125303689](https://ws1.sinaimg.cn/large/006tNc79gy1g20wxyvib7j30n80ggmyz.jpg)
31+
32+
使用栈来解决非递归的后序遍历实现,重点在于以何种顺序入栈,由于是出栈的时候去遍历,所以按照后序遍历的顺序推导出入栈顺序应该是curNode、curNode.right、curNode.left,这样出栈的时候就能保证按照后序遍历的顺序了,另外需要使用一个preNode保存前一个遍历的节点,这样就能确定前一个被遍历的节点是否是当前节点的左/右子节点,如果是,说明其儿子已经被遍历了,这时就可以遍历自己了,否则不能遍历当前节点,需要先将当前节点的子节点入栈,最后的逻辑是:
33+
34+
```
35+
初始:curNode=null,preNode=null,stack=new Stack(),stack.push(root)
36+
while(stack不为空):
37+
if curNode==null:
38+
curNode=stack.peek()
39+
if curNode 是叶子节点:
40+
print curNode.val
41+
preNode=curNode
42+
stack.pop()
43+
curNode=null
44+
else if preNode是curNode的直接子节点:
45+
print curNode.val
46+
preNode=curNode
47+
stack.pop()
48+
curNode=null
49+
else:
50+
stack.push(curNode.right)
51+
stack.push(curNode.left)
52+
curNode=curNode.left
53+
```
54+
55+
56+
57+
## 代码
58+
59+
[这里](../src/n/Solution.java)
Binary file not shown.
350 Bytes
Binary file not shown.

LeetCode/src/six/Solution.java

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package six;
2+
3+
import java.util.ArrayList;
4+
import java.util.Stack;
5+
6+
/**
7+
* @author dmrfcoder
8+
* @date 2019/4/10
9+
*/
10+
11+
12+
class TreeNode {
13+
int val;
14+
TreeNode left;
15+
TreeNode right;
16+
17+
TreeNode(int x) {
18+
val = x;
19+
}
20+
}
21+
22+
public class Solution {
23+
public ArrayList<Integer> postorderTraversal(TreeNode root) {
24+
Stack<TreeNode> treeNodeStack = new Stack<>();
25+
ArrayList<Integer> arrayList = new ArrayList<>();
26+
if (root == null) {
27+
return arrayList;
28+
}
29+
30+
TreeNode preNode = null;
31+
TreeNode curNode = null;
32+
33+
treeNodeStack.push(root);
34+
35+
while (!treeNodeStack.isEmpty()) {
36+
if (curNode == null) {
37+
curNode = treeNodeStack.peek();
38+
39+
}
40+
41+
if (curNode.left == null && curNode.right == null) {
42+
preNode = curNode;
43+
arrayList.add(curNode.val);
44+
treeNodeStack.pop();
45+
curNode = null;
46+
} else if ((preNode == curNode.left && curNode.left != null) || (preNode == curNode.right && curNode.right != null)) {
47+
preNode = curNode;
48+
arrayList.add(curNode.val);
49+
treeNodeStack.pop();
50+
curNode = null;
51+
} else {
52+
if (curNode.right != null) {
53+
treeNodeStack.push(curNode.right);
54+
}
55+
56+
if (curNode.left != null) {
57+
treeNodeStack.push(curNode.left);
58+
}
59+
curNode = curNode.left;
60+
}
61+
62+
63+
}
64+
return arrayList;
65+
}
66+
67+
public static void main(String args[]) {
68+
TreeNode root = new TreeNode(1);
69+
TreeNode node2 = new TreeNode(2);
70+
TreeNode node3 = new TreeNode(3);
71+
TreeNode node4 = new TreeNode(4);
72+
TreeNode node5 = new TreeNode(5);
73+
TreeNode node6 = new TreeNode(6);
74+
TreeNode node7 = new TreeNode(7);
75+
76+
root.left = node2;
77+
// root.right = node3;
78+
// node2.left = node4;
79+
// node2.right = node5;
80+
// node3.left = node6;
81+
// node3.right = node7;
82+
83+
Solution solution = new Solution();
84+
ArrayList<Integer> arrayList = solution.postorderTraversal(root);
85+
System.out.println(arrayList.toArray().toString());
86+
87+
}
88+
}

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,5 @@
147147
- [max-points-on-a-line(同一条直线上的最多的点的数量)](./LeetCode/Doc/同一条直线上的最多的点的数量.md)
148148
- [sort-list(排序List)](./LeetCode/Doc/排序List.md)
149149
- [insertion-sort-list(使用插入排序对链表进行排序)](./LeetCode/Doc/使用插入排序对链表进行排序.md)
150+
- [binary-tree-postorder-traversal(后序遍历二叉树)](./LeetCode/Doc/后序遍历二叉树.md)
150151

0 commit comments

Comments
 (0)