-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathNAryTreePostorderTraversal.java
More file actions
175 lines (162 loc) · 4.99 KB
/
Copy pathNAryTreePostorderTraversal.java
File metadata and controls
175 lines (162 loc) · 4.99 KB
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
/**
* <p>给定一个 N 叉树,返回其节点值的<strong> 后序遍历</strong> 。</p>
*
* <p>N 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 <code>null</code> 分隔(请参见示例)。</p>
*
* <div class="original__bRMd">
* <div>
* <p> </p>
*
* <p><strong>进阶:</strong></p>
*
* <p>递归法很简单,你可以使用迭代法完成此题吗?</p>
*
* <p> </p>
*
* <p><strong>示例 1:</strong></p>
*
* <p><img src="https://assets.leetcode.com/uploads/2018/10/12/narytreeexample.png" style="width: 100%; max-width: 300px;" /></p>
*
* <pre>
* <strong>输入:</strong>root = [1,null,3,2,4,null,5,6]
* <strong>输出:</strong>[5,6,3,2,4,1]
* </pre>
*
* <p><strong>示例 2:</strong></p>
*
* <p><img alt="" src="https://assets.leetcode.com/uploads/2019/11/08/sample_4_964.png" style="width: 296px; height: 241px;" /></p>
*
* <pre>
* <strong>输入:</strong>root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
* <strong>输出:</strong>[2,6,14,11,7,3,12,8,4,13,9,10,5,1]
* </pre>
*
* <p> </p>
*
* <p><strong>提示:</strong></p>
*
* <ul>
* <li>N 叉树的高度小于或等于 <code>1000</code></li>
* <li>节点总数在范围 <code>[0, 10^4]</code> 内</li>
* </ul>
* </div>
* </div>
* <div><div>Related Topics</div><div><li>栈</li><li>树</li><li>深度优先搜索</li></div></div><br><div><li>👍 169</li><li>👎 0</li></div>
*/
package leetcode2;
import java.util.*;
public class NAryTreePostorderTraversal {
public static void main(String[] args) {
}
class Node {
public int val;
public List<Node> children;
public Node() {
}
public Node(int _val) {
val = _val;
}
public Node(int _val, List<Node> _children) {
val = _val;
children = _children;
}
}
/**
* 栈 - 存放 node + value
*/
class Solution1_1 {
public List<Integer> postorder(Node root) {
List<Integer> res = new ArrayList<>();
if (root == null) {
return res;
}
ArrayDeque<Object> stack = new ArrayDeque<>();
stack.push(root);
while (!stack.isEmpty()) {
Object obj = stack.pop();
if (obj instanceof Integer) {
res.add((Integer) obj);
} else {
Node node = (Node) obj;
stack.push(node.val);
if (node.children != null) {
Collections.reverse(node.children);
for (Node child : node.children) {
stack.push(child);
}
}
}
}
return res;
}
}
class Solution1_2 {
public List<Integer> postorder(Node root) {
List<Integer> res = new ArrayList<>();
ArrayDeque<Object> stack = new ArrayDeque<>();
if (root != null) {
stack.push(root);
}
while (!stack.isEmpty()) {
Object obj = stack.pop();
if (obj instanceof Node) {
stack.push(((Node) obj).val);
if (((Node) obj).children != null) {
Collections.reverse(((Node) obj).children);
for (Node child : ((Node) obj).children) {
stack.push(child);
}
}
} else if (obj instanceof Integer) {
res.add((Integer) obj);
}
}
return res;
}
}
/**
* 递归
*/
class Solution2 {
public List<Integer> postorder(Node root) {
List<Integer> res = new ArrayList<>();
solve(root, res);
return res;
}
private void solve(Node root, List<Integer> res) {
if (root == null) {
return;
}
if (root.children == null) {
return;
}
for (Node child : root.children) {
solve(child, res);
}
res.add(root.val);
}
}
/**
* 栈 - 仅存放 node
* 对于每一个Node:res开头添加val,子节点列表正序入栈
*/
class Solution3 {
public List<Integer> postorder(Node root) {
LinkedList<Integer> res = new LinkedList<>();
ArrayDeque<Node> stack = new ArrayDeque<>();
if (root != null) {
stack.push(root);
}
while (!stack.isEmpty()) {
Node node = stack.pop();
res.addFirst(node.val);
if (node.children != null) {
for (Node child : node.children) {
stack.push(child);
}
}
}
return res;
}
}
}