Skip to content

Commit 1411a1d

Browse files
committed
feat: update solutions to lc problem: No.0590
No.0590.N-ary Tree Postorder Traversal
1 parent cf01642 commit 1411a1d

File tree

6 files changed

+67
-127
lines changed

6 files changed

+67
-127
lines changed

solution/0500-0599/0590.N-ary Tree Postorder Traversal/README.md

+23-30
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,18 @@ class Node:
9090
self.children = children
9191
"""
9292

93+
9394
class Solution:
9495
def postorder(self, root: 'Node') -> List[int]:
96+
ans = []
9597
if root is None:
96-
return []
98+
return ans
9799
stk = [root]
98-
ans = []
99100
while stk:
100101
node = stk.pop()
101102
ans.append(node.val)
102-
if node.children:
103-
for child in node.children:
104-
stk.append(child)
103+
for child in node.children:
104+
stk.append(child)
105105
return ans[::-1]
106106
```
107107

@@ -177,27 +177,23 @@ class Node {
177177
*/
178178

179179
class Solution {
180-
181180
public List<Integer> postorder(Node root) {
181+
LinkedList<Integer> ans = new LinkedList<>();
182182
if (root == null) {
183-
return Collections.emptyList();
183+
return ans;
184184
}
185185
Deque<Node> stk = new ArrayDeque<>();
186-
stk.offerLast(root);
187-
LinkedList<Integer> ans = new LinkedList<>();
186+
stk.offer(root);
188187
while (!stk.isEmpty()) {
189-
Node node = stk.pollLast();
190-
ans.addFirst(node.val);
191-
if (node.children != null) {
192-
for (Node child : node.children) {
193-
stk.offerLast(child);
194-
}
188+
root = stk.pollLast();
189+
ans.addFirst(root.val);
190+
for (Node child : root.children) {
191+
stk.offer(child);
195192
}
196193
}
197194
return ans;
198195
}
199196
}
200-
201197
```
202198

203199
### **C++**
@@ -267,17 +263,15 @@ public:
267263
class Solution {
268264
public:
269265
vector<int> postorder(Node* root) {
270-
if (!root) return {};
271-
stack<Node*> stk;
272-
stk.push(root);
273266
vector<int> ans;
267+
if (!root) return ans;
268+
stack<Node*> stk{{root}};
274269
while (!stk.empty())
275270
{
276-
auto& node = stk.top();
271+
root = stk.top();
272+
ans.push_back(root->val);
277273
stk.pop();
278-
ans.push_back(node->val);
279-
for (auto& child : node->children)
280-
stk.push(child);
274+
for (Node* child : root->children) stk.push(child);
281275
}
282276
reverse(ans.begin(), ans.end());
283277
return ans;
@@ -327,17 +321,16 @@ func postorder(root *Node) []int {
327321
*/
328322

329323
func postorder(root *Node) []int {
324+
var ans []int
330325
if root == nil {
331-
return []int{}
326+
return ans
332327
}
333-
var stk []*Node
334-
var ans []int
335-
stk = append(stk, root)
328+
stk := []*Node{root}
336329
for len(stk) > 0 {
337-
node := stk[len(stk)-1]
330+
root = stk[len(stk)-1]
338331
stk = stk[:len(stk)-1]
339-
ans = append([]int{node.Val}, ans...)
340-
for _, child := range node.Children {
332+
ans = append([]int{root.Val}, ans...)
333+
for _, child := range root.Children {
341334
stk = append(stk, child)
342335
}
343336
}

solution/0500-0599/0590.N-ary Tree Postorder Traversal/README_EN.md

+22-69
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ class Node:
7373
self.children = children
7474
"""
7575

76+
7677
class Solution:
7778
def postorder(self, root: 'Node') -> List[int]:
79+
ans = []
7880
if root is None:
79-
return []
81+
return ans
8082
stk = [root]
81-
ans = []
8283
while stk:
8384
node = stk.pop()
8485
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)
8888
return ans[::-1]
8989
```
9090

@@ -154,61 +154,17 @@ class Node {
154154

155155
class Solution {
156156
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);
162157
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) {
199158
if (root == null) {
200-
return Collections.emptyList();
159+
return ans;
201160
}
202161
Deque<Node> stk = new ArrayDeque<>();
203-
stk.offerLast(root);
204-
LinkedList<Integer> ans = new LinkedList<>();
162+
stk.offer(root);
205163
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);
212168
}
213169
}
214170
return ans;
@@ -279,17 +235,15 @@ public:
279235
class Solution {
280236
public:
281237
vector<int> postorder(Node* root) {
282-
if (!root) return {};
283-
stack<Node*> stk;
284-
stk.push(root);
285238
vector<int> ans;
239+
if (!root) return ans;
240+
stack<Node*> stk{{root}};
286241
while (!stk.empty())
287242
{
288-
auto& node = stk.top();
243+
root = stk.top();
244+
ans.push_back(root->val);
289245
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);
293247
}
294248
reverse(ans.begin(), ans.end());
295249
return ans;
@@ -335,17 +289,16 @@ func postorder(root *Node) []int {
335289
*/
336290

337291
func postorder(root *Node) []int {
292+
var ans []int
338293
if root == nil {
339-
return []int{}
294+
return ans
340295
}
341-
var stk []*Node
342-
var ans []int
343-
stk = append(stk, root)
296+
stk := []*Node{root}
344297
for len(stk) > 0 {
345-
node := stk[len(stk)-1]
298+
root = stk[len(stk)-1]
346299
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 {
349302
stk = append(stk, child)
350303
}
351304
}

solution/0500-0599/0590.N-ary Tree Postorder Traversal/Solution.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,15 @@ class Node {
2121
class Solution {
2222
public:
2323
vector<int> postorder(Node* root) {
24-
if (!root) return {};
25-
stack<Node*> stk;
26-
stk.push(root);
2724
vector<int> ans;
25+
if (!root) return ans;
26+
stack<Node*> stk{{root}};
2827
while (!stk.empty())
2928
{
30-
auto& node = stk.top();
29+
root = stk.top();
30+
ans.push_back(root->val);
3131
stk.pop();
32-
ans.push_back(node->val);
33-
for (auto& child : node->children)
34-
stk.push(child);
32+
for (Node* child : root->children) stk.push(child);
3533
}
3634
reverse(ans.begin(), ans.end());
3735
return ans;

solution/0500-0599/0590.N-ary Tree Postorder Traversal/Solution.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
*/
88

99
func postorder(root *Node) []int {
10+
var ans []int
1011
if root == nil {
11-
return []int{}
12+
return ans
1213
}
13-
var stk []*Node
14-
var ans []int
15-
stk = append(stk, root)
14+
stk := []*Node{root}
1615
for len(stk) > 0 {
17-
node := stk[len(stk)-1]
16+
root = stk[len(stk)-1]
1817
stk = stk[:len(stk)-1]
19-
ans = append([]int{node.Val}, ans...)
20-
for _, child := range node.Children {
18+
ans = append([]int{root.Val}, ans...)
19+
for _, child := range root.Children {
2120
stk = append(stk, child)
2221
}
2322
}

solution/0500-0599/0590.N-ary Tree Postorder Traversal/Solution.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,17 @@ public Node(int _val, List<Node> _children) {
1919

2020
class Solution {
2121
public List<Integer> postorder(Node root) {
22+
LinkedList<Integer> ans = new LinkedList<>();
2223
if (root == null) {
23-
return Collections.emptyList();
24+
return ans;
2425
}
2526
Deque<Node> stk = new ArrayDeque<>();
26-
stk.offerLast(root);
27-
LinkedList<Integer> ans = new LinkedList<>();
27+
stk.offer(root);
2828
while (!stk.isEmpty()) {
29-
Node node = stk.pollLast();
30-
ans.addFirst(node.val);
31-
if (node.children != null) {
32-
for (Node child : node.children) {
33-
stk.offerLast(child);
34-
}
29+
root = stk.pollLast();
30+
ans.addFirst(root.val);
31+
for (Node child : root.children) {
32+
stk.offer(child);
3533
}
3634
}
3735
return ans;

solution/0500-0599/0590.N-ary Tree Postorder Traversal/Solution.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ def __init__(self, val=None, children=None):
99

1010
class Solution:
1111
def postorder(self, root: 'Node') -> List[int]:
12+
ans = []
1213
if root is None:
13-
return []
14+
return ans
1415
stk = [root]
15-
ans = []
1616
while stk:
1717
node = stk.pop()
1818
ans.append(node.val)
19-
if node.children:
20-
for child in node.children:
21-
stk.append(child)
19+
for child in node.children:
20+
stk.append(child)
2221
return ans[::-1]

0 commit comments

Comments
 (0)