Day6. Namespace style

This commit is contained in:
Electrominch
2022-10-07 16:36:27 +03:00
parent 62104da8f1
commit 5cae083979
51 changed files with 589 additions and 202 deletions

View File

@@ -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>

View File

@@ -0,0 +1,9 @@
namespace _102._Binary_Tree_Level_Order_Traversal;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}

View 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);
}
}

View 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;
}
}