forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_589.java
49 lines (46 loc) · 1.02 KB
/
_589.java
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
42
43
44
45
46
47
48
49
package com.fishercoder.solutions;
import com.fishercoder.common.classes.Node;
import java.util.ArrayList;
import java.util.List;
/**
* 589. N-ary Tree Preorder Traversal
*
* Given an n-ary tree, return the preorder traversal of its nodes' values.
*
* For example, given a 3-ary tree:
*
* 1
* / | \
* 3 2 4
* / \
* 5 6
*
* Return its preorder traversal as: [1,3,5,6,2,4].
*
* Note:
*
* Recursive solution is trivial, could you do it iteratively?
*/
public class _589 {
public static class Solution1 {
public List<Integer> preorder(Node root) {
List<Integer> result = new ArrayList<>();
if (root == null) {
return result;
}
dfs(root, result);
return result;
}
private void dfs(Node root, List<Integer> result) {
if (root == null) {
return;
}
result.add(root.val);
if (root.children.size() > 0) {
for (Node child : root.children) {
dfs(child, result);
}
}
}
}
}