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>_102._Binary_Tree_Level_Order_Traversal</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
9
102. Binary Tree Level Order Traversal/Program.cs
Normal file
9
102. Binary Tree Level Order Traversal/Program.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace _102._Binary_Tree_Level_Order_Traversal;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("Hello, World!");
|
||||
}
|
||||
}
|
||||
39
102. Binary Tree Level Order Traversal/Solution.cs
Normal file
39
102. Binary Tree Level Order Traversal/Solution.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
namespace _102._Binary_Tree_Level_Order_Traversal;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public IList<IList<int>> LevelOrder(TreeNode root)
|
||||
{
|
||||
List<IList<int>> output = new List<IList<int>>();
|
||||
RecursiveTraversal(output, new List<TreeNode>() { root });
|
||||
return output;
|
||||
}
|
||||
|
||||
private void RecursiveTraversal(List<IList<int>> output, List<TreeNode> curLevel)
|
||||
{
|
||||
if (curLevel.Count == 0)
|
||||
return;
|
||||
List<int> levelValues = new List<int>();
|
||||
foreach (var n in curLevel)
|
||||
{
|
||||
if (n == null)
|
||||
continue;
|
||||
levelValues.Add(n.val);
|
||||
}
|
||||
if (levelValues.Count == 0)
|
||||
return;
|
||||
output.Add(levelValues);
|
||||
|
||||
List<TreeNode> nextLevel = new List<TreeNode>();
|
||||
foreach (var n in curLevel)
|
||||
{
|
||||
if (n == null)
|
||||
continue;
|
||||
if (n.left != null)
|
||||
nextLevel.Add(n.left);
|
||||
if (n.right != null)
|
||||
nextLevel.Add(n.right);
|
||||
}
|
||||
RecursiveTraversal(output, nextLevel);
|
||||
}
|
||||
}
|
||||
17
102. Binary Tree Level Order Traversal/TreeNode.cs
Normal file
17
102. Binary Tree Level Order Traversal/TreeNode.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace _102._Binary_Tree_Level_Order_Traversal;
|
||||
|
||||
|
||||
//Definition for a binary tree node.
|
||||
public class TreeNode
|
||||
{
|
||||
public int val;
|
||||
public TreeNode? left;
|
||||
public TreeNode? right;
|
||||
public TreeNode(int val = 0, TreeNode? left = null, TreeNode? right = null)
|
||||
{
|
||||
this.val = val;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user