Day6. Namespace style
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_589._N_ary_Tree_Preorder_Traversal</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
21
589. N-ary Tree Preorder Traversal/Node.cs
Normal file
21
589. N-ary Tree Preorder Traversal/Node.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
namespace _589._N_ary_Tree_Preorder_Traversal;
|
||||
|
||||
// Definition for a Node.
|
||||
public class Node
|
||||
{
|
||||
public int val;
|
||||
public IList<Node>? children;
|
||||
|
||||
public Node() { }
|
||||
|
||||
public Node(int _val)
|
||||
{
|
||||
val = _val;
|
||||
}
|
||||
|
||||
public Node(int _val, IList<Node> _children)
|
||||
{
|
||||
val = _val;
|
||||
children = _children;
|
||||
}
|
||||
}
|
||||
9
589. N-ary Tree Preorder Traversal/Program.cs
Normal file
9
589. N-ary Tree Preorder Traversal/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _589._N_ary_Tree_Preorder_Traversal;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
22
589. N-ary Tree Preorder Traversal/Solution.cs
Normal file
22
589. N-ary Tree Preorder Traversal/Solution.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
namespace _589._N_ary_Tree_Preorder_Traversal;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public IList<int> Preorder(Node root)
|
||||
{
|
||||
List<int> list = new List<int>();
|
||||
RecursiveTraversal(list, root);
|
||||
return list;
|
||||
}
|
||||
|
||||
private void RecursiveTraversal(List<int> output, Node cur)
|
||||
{
|
||||
if (cur == null)
|
||||
return;
|
||||
output.Add(cur.val);
|
||||
if (cur.children == null)
|
||||
return;
|
||||
foreach (var child in cur.children)
|
||||
RecursiveTraversal(output, child);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user