Skip to content

Commit a495988

Browse files
committed
#590 N叉树后序遍历,递归版本和栈迭代版本
1 parent 20967ca commit a495988

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package tree;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Stack;
7+
8+
/**
9+
* @author alis
10+
* @date 2019/10/27 1:34 AM
11+
* @description
12+
*/
13+
public class LC_590_NTreePostOrderTraversal {
14+
15+
public List<Integer> postOrderRecur(NTreeNode root) {
16+
List<Integer> nums = new ArrayList<>();
17+
if (root == null) {
18+
return nums;
19+
}
20+
if (root.children != null && !root.children.isEmpty()) {
21+
for (NTreeNode child : root.children) {
22+
nums.addAll(postOrderRecur(child));
23+
}
24+
}
25+
nums.add(root.val);
26+
return nums;
27+
}
28+
29+
30+
/**
31+
* 利用栈实现后序遍历
32+
* 核心思想:确定栈的入栈顺序
33+
* 先将根结点栈,在判断子节点是否为空或者上一个输出节点是否为当前节点的子节点
34+
* 如果是,则输出本栈顶节点
35+
* 否则将本节点的子节点从右往左的顺序入栈
36+
*/
37+
public List<Integer> postOrderStack(NTreeNode root) {
38+
List<Integer> nums = new ArrayList<>();
39+
Stack<NTreeNode> stack = new Stack<>();
40+
stack.push(root);
41+
NTreeNode lastNode = null;
42+
while (!stack.isEmpty()) {
43+
NTreeNode cur = stack.peek();
44+
if (cur.children.size() == 0 ||
45+
(lastNode != null && cur.children.contains(lastNode))) {
46+
// 此为核心终结条件; 输出本节点的时候,判断子节点是否输出或者是否有子节点,
47+
// 如果没有子节点,可以直接输出自身
48+
// 如果有子节点,并且上一个输出不是自己的子节点,则需要先输出子节点,将子节点入栈
49+
// 自己第一做的时候,这一步没有控制好,导致漏了一些子节点
50+
nums.add(cur.val);
51+
stack.pop();
52+
lastNode = cur;
53+
} else {
54+
Collections.reverse(cur.children);
55+
cur.children.forEach(stack::push);
56+
}
57+
}
58+
return nums;
59+
}
60+
61+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package tree;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.Collections;
7+
import java.util.List;
8+
9+
import static org.hamcrest.core.Is.is;
10+
import static org.junit.Assert.assertThat;
11+
12+
/**
13+
* @author alis
14+
* @date 2019/10/27 1:30 AM
15+
* @description
16+
*/
17+
public class LC_590_NTreePostOrderTraversalTest {
18+
private LC_590_NTreePostOrderTraversal nTreePostOrderTraversal = new LC_590_NTreePostOrderTraversal();
19+
20+
@Test
21+
public void postOrderTest() {
22+
NTreeNode node2 = new NTreeNode(2, Collections.emptyList());
23+
NTreeNode node4 = new NTreeNode(4, Collections.emptyList());
24+
NTreeNode node3 = new NTreeNode(3, Arrays.asList(new NTreeNode(5, Collections.emptyList()), new NTreeNode(6, Collections.emptyList())));
25+
NTreeNode root = new NTreeNode(1, Arrays.asList(node3, node2, node4));
26+
List<Integer> res = nTreePostOrderTraversal.postOrderRecur(root);
27+
List<Integer> excepted = Arrays.asList(5, 6, 3, 2, 4, 1);
28+
assertThat(res, is(excepted));
29+
}
30+
31+
@Test
32+
public void postOrderStackTest() {
33+
NTreeNode node2 = new NTreeNode(2, Collections.emptyList());
34+
NTreeNode node4 = new NTreeNode(4, Collections.emptyList());
35+
NTreeNode node3 = new NTreeNode(3, Arrays.asList(new NTreeNode(5, Collections.emptyList()), new NTreeNode(6, Collections.emptyList())));
36+
NTreeNode root = new NTreeNode(1, Arrays.asList(node3, node2, node4));
37+
List<Integer> res = nTreePostOrderTraversal.postOrderStack(root);
38+
List<Integer> excepted = Arrays.asList(5, 6, 3, 2, 4, 1);
39+
assertThat(res, is(excepted));
40+
}
41+
42+
@Test
43+
public void postOrderStackErrorCaseTest() {
44+
NTreeNode node3 = new NTreeNode(3, Arrays.asList(new NTreeNode(6, Collections.emptyList())));
45+
NTreeNode node10 = new NTreeNode(10, Arrays.asList(new NTreeNode(5, Collections.emptyList()), new NTreeNode(0, Collections.emptyList())));
46+
NTreeNode root = new NTreeNode(1, Arrays.asList(node10, node3));
47+
List<Integer> res = nTreePostOrderTraversal.postOrderStack(root);
48+
List<Integer> excepted = Arrays.asList(5, 0, 10, 6, 3, 1);
49+
assertThat(res, is(excepted));
50+
}
51+
}

0 commit comments

Comments
 (0)