-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathlc589.java
More file actions
41 lines (35 loc) · 780 Bytes
/
lc589.java
File metadata and controls
41 lines (35 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package code;
import java.util.ArrayList;
import java.util.List;
/*
* 589. N-ary Tree Preorder Traversal
* 题意:多叉树先序遍历
* 难度:Easy
* 分类:Tree
* 思路:
* Tips:
*/
public class lc589 {
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
}
List<Integer> res;
public List<Integer> preorder(Node root) {
res = new ArrayList<>();
helper(root);
return res;
}
public void helper(Node root){
if(root==null) return;
res.add(root.val);
for(Node nd:root.children){
helper(nd);
}
}
}