Skip to content

Commit b9ad39b

Browse files
authored
feat: add solutions to lc problem: No.0590 (#2354)
No.0590.N-ary Tree Postorder Traversal
1 parent 6205ee5 commit b9ad39b

File tree

11 files changed

+138
-127
lines changed

11 files changed

+138
-127
lines changed

solution/0500-0599/0589.N-ary Tree Preorder Traversal/README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -369,10 +369,9 @@ public:
369369
* }
370370
*/
371371
372-
func preorder(root *Node) []int {
373-
var ans []int
372+
func preorder(root *Node) (ans []int) {
374373
if root == nil {
375-
return ans
374+
return
376375
}
377376
stk := []*Node{root}
378377
for len(stk) > 0 {
@@ -384,7 +383,7 @@ func preorder(root *Node) []int {
384383
stk = append(stk, children[i])
385384
}
386385
}
387-
return ans
386+
return
388387
}
389388
```
390389

solution/0500-0599/0589.N-ary Tree Preorder Traversal/README_EN.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,9 @@ public:
365365
* }
366366
*/
367367
368-
func preorder(root *Node) []int {
369-
var ans []int
368+
func preorder(root *Node) (ans []int) {
370369
if root == nil {
371-
return ans
370+
return
372371
}
373372
stk := []*Node{root}
374373
for len(stk) > 0 {
@@ -380,7 +379,7 @@ func preorder(root *Node) []int {
380379
stk = append(stk, children[i])
381380
}
382381
}
383-
return ans
382+
return
384383
}
385384
```
386385

solution/0500-0599/0589.N-ary Tree Preorder Traversal/Solution2.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
* }
77
*/
88

9-
func preorder(root *Node) []int {
10-
var ans []int
9+
func preorder(root *Node) (ans []int) {
1110
if root == nil {
12-
return ans
11+
return
1312
}
1413
stk := []*Node{root}
1514
for len(stk) > 0 {
@@ -21,5 +20,5 @@ func preorder(root *Node) []int {
2120
stk = append(stk, children[i])
2221
}
2322
}
24-
return ans
23+
return
2524
}

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

+47-39
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@
4646

4747
## 解法
4848

49-
### 方法一
49+
### 方法一:递归
50+
51+
我们可以递归地遍历整棵树。对于每个节点,先对该节点的每个子节点递归地调用函数,然后将节点的值加入答案。
52+
53+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点数。
5054

5155
<!-- tabs:start -->
5256

@@ -95,11 +99,9 @@ class Node {
9599
*/
96100

97101
class Solution {
98-
99-
private List<Integer> ans;
102+
private List<Integer> ans = new ArrayList<>();
100103

101104
public List<Integer> postorder(Node root) {
102-
ans = new ArrayList<>();
103105
dfs(root);
104106
return ans;
105107
}
@@ -141,15 +143,18 @@ class Solution {
141143
public:
142144
vector<int> postorder(Node* root) {
143145
vector<int> ans;
144-
dfs(root, ans);
146+
function<void(Node*)> dfs = [&](Node* root) {
147+
if (!root) {
148+
return;
149+
}
150+
for (auto& child : root->children) {
151+
dfs(child);
152+
}
153+
ans.push_back(root->val);
154+
};
155+
dfs(root);
145156
return ans;
146157
}
147-
148-
void dfs(Node* root, vector<int>& ans) {
149-
if (!root) return;
150-
for (auto& child : root->children) dfs(child, ans);
151-
ans.push_back(root->val);
152-
}
153158
};
154159
```
155160
@@ -162,9 +167,8 @@ public:
162167
* }
163168
*/
164169
165-
func postorder(root *Node) []int {
166-
var ans []int
167-
var dfs func(root *Node)
170+
func postorder(root *Node) (ans []int) {
171+
var dfs func(*Node)
168172
dfs = func(root *Node) {
169173
if root == nil {
170174
return
@@ -175,7 +179,7 @@ func postorder(root *Node) []int {
175179
ans = append(ans, root.Val)
176180
}
177181
dfs(root)
178-
return ans
182+
return
179183
}
180184
```
181185

@@ -193,24 +197,30 @@ func postorder(root *Node) []int {
193197
*/
194198

195199
function postorder(root: Node | null): number[] {
196-
const res = [];
200+
const ans: number[] = [];
197201
const dfs = (root: Node | null) => {
198-
if (root == null) {
202+
if (!root) {
199203
return;
200204
}
201-
for (const node of root.children) {
202-
dfs(node);
205+
for (const child of root.children) {
206+
dfs(child);
203207
}
204-
res.push(root.val);
208+
ans.push(root.val);
205209
};
206210
dfs(root);
207-
return res;
211+
return ans;
208212
}
209213
```
210214

211215
<!-- tabs:end -->
212216

213-
### 方法二
217+
### 方法二:迭代(栈实现)
218+
219+
我们也可以用迭代的方法来解决这个问题。
220+
221+
我们使用一个栈来帮助我们得到后序遍历,我们首先把根节点入栈,因为后序遍历是左子树、右子树、根节点,栈的特点是先进后出,所以我们先把节点的值加入答案,然后对该节点的每个子节点按照从左到右的顺序依次入栈,这样可以得到根节点、右子树、左子树的遍历结果。最后把答案反转即可得到后序遍历的结果。
222+
223+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点数。
214224

215225
<!-- tabs:start -->
216226

@@ -303,13 +313,17 @@ class Solution {
303313
public:
304314
vector<int> postorder(Node* root) {
305315
vector<int> ans;
306-
if (!root) return ans;
316+
if (!root) {
317+
return ans;
318+
}
307319
stack<Node*> stk{{root}};
308320
while (!stk.empty()) {
309321
root = stk.top();
310322
ans.push_back(root->val);
311323
stk.pop();
312-
for (Node* child : root->children) stk.push(child);
324+
for (Node* child : root->children) {
325+
stk.push(child);
326+
}
313327
}
314328
reverse(ans.begin(), ans.end());
315329
return ans;
@@ -358,23 +372,17 @@ func postorder(root *Node) []int {
358372
*/
359373

360374
function postorder(root: Node | null): number[] {
361-
const res = [];
362-
if (root == null) {
363-
return res;
375+
const ans: number[] = [];
376+
if (!root) {
377+
return ans;
364378
}
365-
const stack = [root];
366-
while (stack.length !== 0) {
367-
const target = stack[stack.length - 1];
368-
if (target.children == null) {
369-
res.push(stack.pop().val);
370-
} else {
371-
for (let i = target.children.length - 1; i >= 0; i--) {
372-
stack.push(target.children[i]);
373-
}
374-
target.children = null;
375-
}
379+
const stk: Node[] = [root];
380+
while (stk.length) {
381+
const { val, children } = stk.pop()!;
382+
ans.push(val);
383+
stk.push(...children);
376384
}
377-
return res;
385+
return ans.reverse();
378386
}
379387
```
380388

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

+47-39
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737

3838
## Solutions
3939

40-
### Solution 1
40+
### Solution 1: Recursion
41+
42+
We can recursively traverse the entire tree. For each node, we first recursively call the function for each of the node's children, then add the node's value to the answer.
43+
44+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes.
4145

4246
<!-- tabs:start -->
4347

@@ -86,11 +90,9 @@ class Node {
8690
*/
8791

8892
class Solution {
89-
90-
private List<Integer> ans;
93+
private List<Integer> ans = new ArrayList<>();
9194

9295
public List<Integer> postorder(Node root) {
93-
ans = new ArrayList<>();
9496
dfs(root);
9597
return ans;
9698
}
@@ -132,15 +134,18 @@ class Solution {
132134
public:
133135
vector<int> postorder(Node* root) {
134136
vector<int> ans;
135-
dfs(root, ans);
137+
function<void(Node*)> dfs = [&](Node* root) {
138+
if (!root) {
139+
return;
140+
}
141+
for (auto& child : root->children) {
142+
dfs(child);
143+
}
144+
ans.push_back(root->val);
145+
};
146+
dfs(root);
136147
return ans;
137148
}
138-
139-
void dfs(Node* root, vector<int>& ans) {
140-
if (!root) return;
141-
for (auto& child : root->children) dfs(child, ans);
142-
ans.push_back(root->val);
143-
}
144149
};
145150
```
146151
@@ -153,9 +158,8 @@ public:
153158
* }
154159
*/
155160
156-
func postorder(root *Node) []int {
157-
var ans []int
158-
var dfs func(root *Node)
161+
func postorder(root *Node) (ans []int) {
162+
var dfs func(*Node)
159163
dfs = func(root *Node) {
160164
if root == nil {
161165
return
@@ -166,7 +170,7 @@ func postorder(root *Node) []int {
166170
ans = append(ans, root.Val)
167171
}
168172
dfs(root)
169-
return ans
173+
return
170174
}
171175
```
172176

@@ -184,24 +188,30 @@ func postorder(root *Node) []int {
184188
*/
185189

186190
function postorder(root: Node | null): number[] {
187-
const res = [];
191+
const ans: number[] = [];
188192
const dfs = (root: Node | null) => {
189-
if (root == null) {
193+
if (!root) {
190194
return;
191195
}
192-
for (const node of root.children) {
193-
dfs(node);
196+
for (const child of root.children) {
197+
dfs(child);
194198
}
195-
res.push(root.val);
199+
ans.push(root.val);
196200
};
197201
dfs(root);
198-
return res;
202+
return ans;
199203
}
200204
```
201205

202206
<!-- tabs:end -->
203207

204-
### Solution 2
208+
### Solution 2: Iteration (Stack Implementation)
209+
210+
We can also solve this problem iteratively.
211+
212+
We use a stack to help us get the post-order traversal. We first push the root node into the stack. Since the post-order traversal is left subtree, right subtree, root, and the characteristic of the stack is first in last out, we first add the node's value to the answer, then push each of the node's children into the stack in the order from left to right. This way, we can get the traversal result of root, right subtree, left subtree. Finally, we reverse the answer to get the post-order traversal result.
213+
214+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the number of nodes.
205215

206216
<!-- tabs:start -->
207217

@@ -294,13 +304,17 @@ class Solution {
294304
public:
295305
vector<int> postorder(Node* root) {
296306
vector<int> ans;
297-
if (!root) return ans;
307+
if (!root) {
308+
return ans;
309+
}
298310
stack<Node*> stk{{root}};
299311
while (!stk.empty()) {
300312
root = stk.top();
301313
ans.push_back(root->val);
302314
stk.pop();
303-
for (Node* child : root->children) stk.push(child);
315+
for (Node* child : root->children) {
316+
stk.push(child);
317+
}
304318
}
305319
reverse(ans.begin(), ans.end());
306320
return ans;
@@ -349,23 +363,17 @@ func postorder(root *Node) []int {
349363
*/
350364

351365
function postorder(root: Node | null): number[] {
352-
const res = [];
353-
if (root == null) {
354-
return res;
366+
const ans: number[] = [];
367+
if (!root) {
368+
return ans;
355369
}
356-
const stack = [root];
357-
while (stack.length !== 0) {
358-
const target = stack[stack.length - 1];
359-
if (target.children == null) {
360-
res.push(stack.pop().val);
361-
} else {
362-
for (let i = target.children.length - 1; i >= 0; i--) {
363-
stack.push(target.children[i]);
364-
}
365-
target.children = null;
366-
}
370+
const stk: Node[] = [root];
371+
while (stk.length) {
372+
const { val, children } = stk.pop()!;
373+
ans.push(val);
374+
stk.push(...children);
367375
}
368-
return res;
376+
return ans.reverse();
369377
}
370378
```
371379

0 commit comments

Comments
 (0)