@@ -73,18 +73,18 @@ class Node:
73
73
self.children = children
74
74
"""
75
75
76
+
76
77
class Solution :
77
78
def postorder (self , root : ' Node' ) -> List[int ]:
79
+ ans = []
78
80
if root is None :
79
- return []
81
+ return ans
80
82
stk = [root]
81
- ans = []
82
83
while stk:
83
84
node = stk.pop()
84
85
ans.append(node.val)
85
- if node.children:
86
- for child in node.children:
87
- stk.append(child)
86
+ for child in node.children:
87
+ stk.append(child)
88
88
return ans[::- 1 ]
89
89
```
90
90
@@ -154,61 +154,17 @@ class Node {
154
154
155
155
class Solution {
156
156
public List<Integer > postorder (Node root ) {
157
- if (root == null ) {
158
- return Collections . emptyList();
159
- }
160
- Deque<Node > stk = new ArrayDeque<> ();
161
- stk. offerLast(root);
162
157
LinkedList<Integer > ans = new LinkedList<> ();
163
- while (! stk. isEmpty()) {
164
- Node node = stk. pollLast();
165
- ans. addFirst(node. val);
166
- if (node. children != null ) {
167
- for (Node child : node. children) {
168
- stk. offerLast(child);
169
- }
170
- }
171
- }
172
- return ans;
173
- }
174
- }
175
- ```
176
-
177
- ``` java
178
- /*
179
- // Definition for a Node.
180
- class Node {
181
- public int val;
182
- public List<Node> children;
183
-
184
- public Node() {}
185
-
186
- public Node(int _val) {
187
- val = _val;
188
- }
189
-
190
- public Node(int _val, List<Node> _children) {
191
- val = _val;
192
- children = _children;
193
- }
194
- };
195
- */
196
-
197
- class Solution {
198
- public List<Integer > postorder (Node root ) {
199
158
if (root == null ) {
200
- return Collections . emptyList() ;
159
+ return ans ;
201
160
}
202
161
Deque<Node > stk = new ArrayDeque<> ();
203
- stk. offerLast(root);
204
- LinkedList<Integer > ans = new LinkedList<> ();
162
+ stk. offer(root);
205
163
while (! stk. isEmpty()) {
206
- Node node = stk. pollLast();
207
- ans. addFirst(node. val);
208
- if (node. children != null ) {
209
- for (Node child : node. children) {
210
- stk. offerLast(child);
211
- }
164
+ root = stk. pollLast();
165
+ ans. addFirst(root. val);
166
+ for (Node child : root. children) {
167
+ stk. offer(child);
212
168
}
213
169
}
214
170
return ans;
@@ -279,17 +235,15 @@ public:
279
235
class Solution {
280
236
public:
281
237
vector<int > postorder(Node* root) {
282
- if (!root) return {};
283
- stack<Node* > stk;
284
- stk.push(root);
285
238
vector<int > ans;
239
+ if (!root) return ans;
240
+ stack<Node* > stk{{root}};
286
241
while (!stk.empty())
287
242
{
288
- auto& node = stk.top();
243
+ root = stk.top();
244
+ ans.push_back(root->val);
289
245
stk.pop();
290
- ans.push_back(node->val);
291
- for (auto& child : node->children)
292
- stk.push(child);
246
+ for (Node* child : root->children) stk.push(child);
293
247
}
294
248
reverse(ans.begin(), ans.end());
295
249
return ans;
@@ -335,17 +289,16 @@ func postorder(root *Node) []int {
335
289
*/
336
290
337
291
func postorder (root *Node ) []int {
292
+ var ans []int
338
293
if root == nil {
339
- return [] int {}
294
+ return ans
340
295
}
341
- var stk []*Node
342
- var ans []int
343
- stk = append (stk, root)
296
+ stk := []*Node{root}
344
297
for len (stk) > 0 {
345
- node : = stk[len (stk)-1 ]
298
+ root = stk[len (stk)-1 ]
346
299
stk = stk[:len (stk)-1 ]
347
- ans = append ([]int {node .Val }, ans...)
348
- for _ , child := range node .Children {
300
+ ans = append ([]int {root .Val }, ans...)
301
+ for _ , child := range root .Children {
349
302
stk = append (stk, child)
350
303
}
351
304
}
0 commit comments