Skip to content

Commit 670708c

Browse files
committed
feat: add solutions to lc problem: No.0589
No.0589.N-ary Tree Preorder Traversal
1 parent da440c1 commit 670708c

File tree

12 files changed

+366
-82
lines changed

12 files changed

+366
-82
lines changed

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

Lines changed: 120 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,134 @@ class Node:
7171

7272
class Solution:
7373
def preorder(self, root: 'Node') -> List[int]:
74-
if not root:
75-
return []
76-
77-
def PO(root):
78-
res.append(root.val)
79-
for i in root.children:
80-
PO(i)
81-
res = []
82-
PO(root)
83-
return res
74+
ans = []
75+
if root is None:
76+
return ans
77+
stk = [root]
78+
while stk:
79+
node = stk.pop()
80+
ans.append(node.val)
81+
for child in node.children[::-1]:
82+
stk.append(child)
83+
return ans
8484
```
8585

8686
### **Java**
8787

8888
<!-- 这里可写当前语言的特殊实现逻辑 -->
8989

9090
```java
91+
/*
92+
// Definition for a Node.
93+
class Node {
94+
public int val;
95+
public List<Node> children;
96+
97+
public Node() {}
98+
99+
public Node(int _val) {
100+
val = _val;
101+
}
102+
103+
public Node(int _val, List<Node> _children) {
104+
val = _val;
105+
children = _children;
106+
}
107+
};
108+
*/
109+
110+
class Solution {
111+
public List<Integer> preorder(Node root) {
112+
if (root == null) {
113+
return Collections.emptyList();
114+
}
115+
List<Integer> ans = new ArrayList<>();
116+
Deque<Node> stk = new ArrayDeque<>();
117+
stk.push(root);
118+
while (!stk.isEmpty()) {
119+
Node node = stk.pop();
120+
ans.add(node.val);
121+
List<Node> children = node.children;
122+
for (int i = children.size() - 1; i >= 0; --i) {
123+
stk.push(children.get(i));
124+
}
125+
}
126+
return ans;
127+
}
128+
}
129+
```
130+
131+
### **C++**
132+
133+
```cpp
134+
/*
135+
// Definition for a Node.
136+
class Node {
137+
public:
138+
int val;
139+
vector<Node*> children;
140+
141+
Node() {}
142+
143+
Node(int _val) {
144+
val = _val;
145+
}
146+
147+
Node(int _val, vector<Node*> _children) {
148+
val = _val;
149+
children = _children;
150+
}
151+
};
152+
*/
153+
154+
class Solution {
155+
public:
156+
vector<int> preorder(Node* root) {
157+
if (!root) return {};
158+
vector<int> ans;
159+
stack<Node*> stk;
160+
stk.push(root);
161+
while (!stk.empty())
162+
{
163+
Node* node = stk.top();
164+
ans.push_back(node->val);
165+
stk.pop();
166+
auto children = node->children;
167+
for (int i = children.size() - 1; i >= 0; --i) stk.push(children[i]);
168+
}
169+
return ans;
170+
}
171+
};
172+
```
91173
174+
### **Go**
175+
176+
```go
177+
/**
178+
* Definition for a Node.
179+
* type Node struct {
180+
* Val int
181+
* Children []*Node
182+
* }
183+
*/
184+
185+
func preorder(root *Node) []int {
186+
var ans []int
187+
if root == nil {
188+
return ans
189+
}
190+
stk := []*Node{root}
191+
for len(stk) > 0 {
192+
node := stk[len(stk)-1]
193+
ans = append(ans, node.Val)
194+
stk = stk[:len(stk)-1]
195+
children := node.Children
196+
for i := len(children) - 1; i >= 0; i-- {
197+
stk = append(stk, children[i])
198+
}
199+
}
200+
return ans
201+
}
92202
```
93203

94204
### **...**

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

Lines changed: 120 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,132 @@ class Node:
5757

5858
class Solution:
5959
def preorder(self, root: 'Node') -> List[int]:
60-
if not root:
61-
return []
62-
63-
def PO(root):
64-
res.append(root.val)
65-
for i in root.children:
66-
PO(i)
67-
res = []
68-
PO(root)
69-
return res
60+
ans = []
61+
if root is None:
62+
return ans
63+
stk = [root]
64+
while stk:
65+
node = stk.pop()
66+
ans.append(node.val)
67+
for child in node.children[::-1]:
68+
stk.append(child)
69+
return ans
7070
```
7171

7272
### **Java**
7373

7474
```java
75+
/*
76+
// Definition for a Node.
77+
class Node {
78+
public int val;
79+
public List<Node> children;
80+
81+
public Node() {}
82+
83+
public Node(int _val) {
84+
val = _val;
85+
}
86+
87+
public Node(int _val, List<Node> _children) {
88+
val = _val;
89+
children = _children;
90+
}
91+
};
92+
*/
93+
94+
class Solution {
95+
public List<Integer> preorder(Node root) {
96+
if (root == null) {
97+
return Collections.emptyList();
98+
}
99+
List<Integer> ans = new ArrayList<>();
100+
Deque<Node> stk = new ArrayDeque<>();
101+
stk.push(root);
102+
while (!stk.isEmpty()) {
103+
Node node = stk.pop();
104+
ans.add(node.val);
105+
List<Node> children = node.children;
106+
for (int i = children.size() - 1; i >= 0; --i) {
107+
stk.push(children.get(i));
108+
}
109+
}
110+
return ans;
111+
}
112+
}
113+
```
114+
115+
### **C++**
116+
117+
```cpp
118+
/*
119+
// Definition for a Node.
120+
class Node {
121+
public:
122+
int val;
123+
vector<Node*> children;
124+
125+
Node() {}
126+
127+
Node(int _val) {
128+
val = _val;
129+
}
130+
131+
Node(int _val, vector<Node*> _children) {
132+
val = _val;
133+
children = _children;
134+
}
135+
};
136+
*/
137+
138+
class Solution {
139+
public:
140+
vector<int> preorder(Node* root) {
141+
if (!root) return {};
142+
vector<int> ans;
143+
stack<Node*> stk;
144+
stk.push(root);
145+
while (!stk.empty())
146+
{
147+
Node* node = stk.top();
148+
ans.push_back(node->val);
149+
stk.pop();
150+
auto children = node->children;
151+
for (int i = children.size() - 1; i >= 0; --i) stk.push(children[i]);
152+
}
153+
return ans;
154+
}
155+
};
156+
```
75157
158+
### **Go**
159+
160+
```go
161+
/**
162+
* Definition for a Node.
163+
* type Node struct {
164+
* Val int
165+
* Children []*Node
166+
* }
167+
*/
168+
169+
func preorder(root *Node) []int {
170+
var ans []int
171+
if root == nil {
172+
return ans
173+
}
174+
stk := []*Node{root}
175+
for len(stk) > 0 {
176+
node := stk[len(stk)-1]
177+
ans = append(ans, node.Val)
178+
stk = stk[:len(stk)-1]
179+
children := node.Children
180+
for i := len(children) - 1; i >= 0; i-- {
181+
stk = append(stk, children[i])
182+
}
183+
}
184+
return ans
185+
}
76186
```
77187

78188
### **...**
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
vector<Node*> children;
7+
8+
Node() {}
9+
10+
Node(int _val) {
11+
val = _val;
12+
}
13+
14+
Node(int _val, vector<Node*> _children) {
15+
val = _val;
16+
children = _children;
17+
}
18+
};
19+
*/
20+
21+
class Solution {
22+
public:
23+
vector<int> preorder(Node* root) {
24+
if (!root) return {};
25+
vector<int> ans;
26+
stack<Node*> stk;
27+
stk.push(root);
28+
while (!stk.empty())
29+
{
30+
Node* node = stk.top();
31+
ans.push_back(node->val);
32+
stk.pop();
33+
auto children = node->children;
34+
for (int i = children.size() - 1; i >= 0; --i) stk.push(children[i]);
35+
}
36+
return ans;
37+
}
38+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a Node.
3+
* type Node struct {
4+
* Val int
5+
* Children []*Node
6+
* }
7+
*/
8+
9+
func preorder(root *Node) []int {
10+
var ans []int
11+
if root == nil {
12+
return ans
13+
}
14+
stk := []*Node{root}
15+
for len(stk) > 0 {
16+
node := stk[len(stk)-1]
17+
ans = append(ans, node.Val)
18+
stk = stk[:len(stk)-1]
19+
children := node.Children
20+
for i := len(children) - 1; i >= 0; i-- {
21+
stk = append(stk, children[i])
22+
}
23+
}
24+
return ans
25+
}

0 commit comments

Comments
 (0)