59
59
60
60
<!-- 这里可写当前语言的特殊实现逻辑 -->
61
61
62
+ 递归:
63
+
62
64
``` python
63
65
"""
64
66
# Definition for a Node.
@@ -68,31 +70,284 @@ class Node:
68
70
self.children = children
69
71
"""
70
72
73
+ class Solution :
74
+ def postorder (self , root : ' Node' ) -> List[int ]:
75
+ def dfs (root ):
76
+ if root is None :
77
+ return
78
+ for child in root.children:
79
+ dfs(child)
80
+ ans.append(root.val)
81
+
82
+ ans = []
83
+ dfs(root)
84
+ return ans
85
+ ```
86
+
87
+ 迭代:
88
+
89
+ ``` python
90
+ """
91
+ # Definition for a Node.
92
+ class Node:
93
+ def __init__(self, val=None, children=None):
94
+ self.val = val
95
+ self.children = children
96
+ """
71
97
72
98
class Solution :
73
99
def postorder (self , root : ' Node' ) -> List[int ]:
74
- if not root:
100
+ if root is None :
75
101
return []
76
-
77
- def PO (root ):
78
- if root == None :
79
- return res
80
- else :
81
- for i in root.children:
82
- PO(i)
83
- res.append(i.val)
84
- res = []
85
- PO(root)
86
- res.append(root.val)
87
- return res
102
+ stk = [root]
103
+ ans = []
104
+ while stk:
105
+ node = stk.pop()
106
+ ans.append(node.val)
107
+ if node.children:
108
+ for child in node.children:
109
+ stk.append(child)
110
+ return ans[::- 1 ]
88
111
```
89
112
90
113
### ** Java**
91
114
92
115
<!-- 这里可写当前语言的特殊实现逻辑 -->
93
116
117
+ 递归:
118
+
94
119
``` java
120
+ /*
121
+ // Definition for a Node.
122
+ class Node {
123
+ public int val;
124
+ public List<Node> children;
125
+
126
+ public Node() {}
127
+
128
+ public Node(int _val) {
129
+ val = _val;
130
+ }
131
+
132
+ public Node(int _val, List<Node> _children) {
133
+ val = _val;
134
+ children = _children;
135
+ }
136
+ };
137
+ */
138
+
139
+ class Solution {
140
+
141
+ private List<Integer > ans;
142
+
143
+ public List<Integer > postorder (Node root ) {
144
+ ans = new ArrayList<> ();
145
+ dfs(root);
146
+ return ans;
147
+ }
148
+
149
+ private void dfs (Node root ) {
150
+ if (root == null ) {
151
+ return ;
152
+ }
153
+ for (Node child : root. children) {
154
+ dfs(child);
155
+ }
156
+ ans. add(root. val);
157
+ }
158
+ }
159
+
160
+ ```
161
+
162
+ 迭代:
163
+
164
+ ``` java
165
+ /*
166
+ // Definition for a Node.
167
+ class Node {
168
+ public int val;
169
+ public List<Node> children;
170
+
171
+ public Node() {}
172
+
173
+ public Node(int _val) {
174
+ val = _val;
175
+ }
176
+
177
+ public Node(int _val, List<Node> _children) {
178
+ val = _val;
179
+ children = _children;
180
+ }
181
+ };
182
+ */
183
+
184
+ class Solution {
185
+
186
+ public List<Integer > postorder (Node root ) {
187
+ if (root == null ) {
188
+ return Collections . emptyList();
189
+ }
190
+ Deque<Node > stk = new ArrayDeque<> ();
191
+ stk. offerLast(root);
192
+ LinkedList<Integer > ans = new LinkedList<> ();
193
+ while (! stk. isEmpty()) {
194
+ Node node = stk. pollLast();
195
+ ans. addFirst(node. val);
196
+ if (node. children != null ) {
197
+ for (Node child : node. children) {
198
+ stk. offerLast(child);
199
+ }
200
+ }
201
+ }
202
+ return ans;
203
+ }
204
+ }
205
+
206
+ ```
207
+
208
+ ### ** C++**
209
+
210
+ 递归:
211
+
212
+ ``` cpp
213
+ /*
214
+ // Definition for a Node.
215
+ class Node {
216
+ public:
217
+ int val;
218
+ vector<Node*> children;
219
+
220
+ Node() {}
221
+
222
+ Node(int _val) {
223
+ val = _val;
224
+ }
225
+
226
+ Node(int _val, vector<Node*> _children) {
227
+ val = _val;
228
+ children = _children;
229
+ }
230
+ };
231
+ */
232
+
233
+ class Solution {
234
+ public:
235
+ vector<int > postorder(Node* root) {
236
+ vector<int > ans;
237
+ dfs(root, ans);
238
+ return ans;
239
+ }
240
+
241
+ void dfs(Node* root, vector<int>& ans) {
242
+ if (!root) return;
243
+ for (auto& child : root->children) dfs(child, ans);
244
+ ans.push_back(root->val);
245
+ }
246
+ };
247
+ ```
248
+
249
+ 迭代:
250
+
251
+ ``` cpp
252
+ /*
253
+ // Definition for a Node.
254
+ class Node {
255
+ public:
256
+ int val;
257
+ vector<Node*> children;
258
+
259
+ Node() {}
260
+
261
+ Node(int _val) {
262
+ val = _val;
263
+ }
264
+
265
+ Node(int _val, vector<Node*> _children) {
266
+ val = _val;
267
+ children = _children;
268
+ }
269
+ };
270
+ */
271
+
272
+ class Solution {
273
+ public:
274
+ vector<int > postorder(Node* root) {
275
+ if (!root) return {};
276
+ stack<Node* > stk;
277
+ stk.push(root);
278
+ vector<int > ans;
279
+ while (!stk.empty())
280
+ {
281
+ auto& node = stk.top();
282
+ stk.pop();
283
+ ans.push_back(node->val);
284
+ for (auto& child : node->children)
285
+ stk.push(child);
286
+ }
287
+ reverse(ans.begin(), ans.end());
288
+ return ans;
289
+ }
290
+ };
291
+ ```
292
+
293
+ ### **Go**
294
+
295
+ 递归:
296
+
297
+ ```go
298
+ /**
299
+ * Definition for a Node.
300
+ * type Node struct {
301
+ * Val int
302
+ * Children []*Node
303
+ * }
304
+ */
305
+
306
+ func postorder(root *Node) []int {
307
+ var ans []int
308
+ var dfs func(root *Node)
309
+ dfs = func(root *Node) {
310
+ if root == nil {
311
+ return
312
+ }
313
+ for _, child := range root.Children {
314
+ dfs(child)
315
+ }
316
+ ans = append(ans, root.Val)
317
+ }
318
+ dfs(root)
319
+ return ans
320
+ }
321
+ ```
95
322
323
+ 迭代:
324
+
325
+ ``` go
326
+ /* *
327
+ * Definition for a Node.
328
+ * type Node struct {
329
+ * Val int
330
+ * Children []*Node
331
+ * }
332
+ */
333
+
334
+ func postorder (root *Node ) []int {
335
+ if root == nil {
336
+ return []int {}
337
+ }
338
+ var stk []*Node
339
+ var ans []int
340
+ stk = append (stk, root)
341
+ for len (stk) > 0 {
342
+ node := stk[len (stk)-1 ]
343
+ stk = stk[:len (stk)-1 ]
344
+ ans = append ([]int {node.Val }, ans...)
345
+ for _ , child := range node.Children {
346
+ stk = append (stk, child)
347
+ }
348
+ }
349
+ return ans
350
+ }
96
351
```
97
352
98
353
### ** ...**
0 commit comments