-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution589.cs
46 lines (43 loc) · 1013 Bytes
/
Solution589.cs
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
namespace LeetCode.Solutions;
public class Solution589
{
public class Node
{
public int val;
public IList<Node> children;
public Node()
{
}
public Node(int val)
{
this.val = val;
}
public Node(int val, IList<Node> children)
{
this.val = val;
this.children = children;
}
}
/// <summary>
/// 589. N-ary Tree Preorder Traversal - Easy
/// <a href="https://leetcode.com/problems/n-ary-tree-preorder-traversal">See the problem</a>
/// </summary>
public IList<int> Preorder(Node root)
{
var result = new List<int>();
Preorder(root, result);
return result;
}
private void Preorder(Node root, List<int> result)
{
if (root == null)
{
return;
}
result.Add(root.val);
foreach (var child in root.children)
{
Preorder(child, result);
}
}
}