-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
46 lines (42 loc) · 1.05 KB
/
Program.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
//如果root为空直接返回null。否则复制root,并复制root的每一个child并加入clone的children。
using System;
using System.Collections.Generic;
namespace CloneNaryTree
{
public class Node
{
public int val;
public IList<Node> children;
public Node()
{
val = 0;
children = new List<Node>();
}
public Node(int _val)
{
val = _val;
children = new List<Node>();
}
public Node(int _val, List<Node> _children)
{
val = _val;
children = _children;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
static Node CloneTree(Node root)
{
if (root == null)
return null;
var clone = new Node(root.val);
foreach (var child in root.children)
clone.children.Add(CloneTree(child));
return clone;
}
}
}