Day10
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_617._Merge_Two_Binary_Trees</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
58
617. Merge Two Binary Trees/Program.cs
Normal file
58
617. Merge Two Binary Trees/Program.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
namespace _617._Merge_Two_Binary_Trees;
|
||||
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
TreeNode root1 = new TreeNode(1);
|
||||
root1.left = new TreeNode(3) { left = new TreeNode(5) };
|
||||
root1.right = new TreeNode(2);
|
||||
|
||||
|
||||
TreeNode root2 = new TreeNode(2);
|
||||
root2.left = new TreeNode(1) { right = new TreeNode(4) };
|
||||
root2.right = new TreeNode(3) { right = new TreeNode(7) };
|
||||
var res = new Write().LevelOrder(new Solution().MergeTrees(root1, root2)!);
|
||||
Console.WriteLine($"\nOutput");
|
||||
foreach (var row in res)
|
||||
Console.WriteLine(String.Join(" ", row));
|
||||
}
|
||||
|
||||
public class Write
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
617. Merge Two Binary Trees/Solution.cs
Normal file
31
617. Merge Two Binary Trees/Solution.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
namespace _617._Merge_Two_Binary_Trees;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public TreeNode? MergeTrees(TreeNode root1, TreeNode root2)
|
||||
{
|
||||
if (root1 == null && root2 == null)
|
||||
return null;
|
||||
else if (root1 == null)
|
||||
return root2;
|
||||
else if (root2 == null)
|
||||
return root1;
|
||||
var res = new TreeNode();
|
||||
RecursionMerge(res, root1, root2);
|
||||
return res;
|
||||
}
|
||||
|
||||
private void RecursionMerge(TreeNode cur, TreeNode? t1, TreeNode? t2)
|
||||
{
|
||||
if (t1 != null)
|
||||
cur.val += t1.val;
|
||||
if (t2 != null)
|
||||
cur.val += t2.val;
|
||||
cur.left = (t1?.left != null || t2?.left != null) ? new TreeNode() : null;
|
||||
cur.right = (t1?.right != null || t2?.right != null) ? new TreeNode() : null;
|
||||
if (cur.left != null)
|
||||
RecursionMerge(cur.left, t1?.left, t2?.left);
|
||||
if (cur.right != null)
|
||||
RecursionMerge(cur.right, t1?.right, t2?.right);
|
||||
}
|
||||
}
|
||||
16
617. Merge Two Binary Trees/TreeNode.cs
Normal file
16
617. Merge Two Binary Trees/TreeNode.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
namespace _617._Merge_Two_Binary_Trees;
|
||||
|
||||
|
||||
//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