114. Flatten Binary Tree to Linked List
This commit is contained in:
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<RootNamespace>_114._Flatten_Binary_Tree_to_Linked_List</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
67
114. Flatten Binary Tree to Linked List/Program.cs
Normal file
67
114. Flatten Binary Tree to Linked List/Program.cs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
var sol = new Solution();
|
||||||
|
|
||||||
|
var cases = new (int?[] input, int?[] expected)[]
|
||||||
|
{
|
||||||
|
(new int?[] { 1, 2, 5, 3, 4, null, 6 }, new int?[] { 1, null, 2, null, 3, null, 4, null, 5, null, 6 }),
|
||||||
|
// (Array.Empty<int?>(), Array.Empty<int?>()),
|
||||||
|
// (new int?[] { 0 }, new int?[] { 0 })
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var (input, expected) in cases)
|
||||||
|
{
|
||||||
|
var root = BuildTreeFromLevelOrder(input);
|
||||||
|
if (root != null)
|
||||||
|
sol.Flatten(root);
|
||||||
|
|
||||||
|
var actual = FlattenToOutputList(root);
|
||||||
|
Console.WriteLine($"[{string.Join(", ", input.Select(FormatNullable))}] -> [{string.Join(", ", actual.Select(FormatNullable))}] (expected: [{string.Join(", ", expected.Select(FormatNullable))}])");
|
||||||
|
}
|
||||||
|
|
||||||
|
static TreeNode? BuildTreeFromLevelOrder(int?[] data)
|
||||||
|
{
|
||||||
|
if (data.Length == 0 || data[0] == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var root = new TreeNode(data[0].Value);
|
||||||
|
var queue = new Queue<TreeNode>();
|
||||||
|
queue.Enqueue(root);
|
||||||
|
|
||||||
|
var i = 1;
|
||||||
|
while (queue.Count > 0 && i < data.Length)
|
||||||
|
{
|
||||||
|
var node = queue.Dequeue();
|
||||||
|
|
||||||
|
if (i < data.Length && data[i] != null)
|
||||||
|
{
|
||||||
|
node.left = new TreeNode(data[i]!.Value);
|
||||||
|
queue.Enqueue(node.left);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
|
||||||
|
if (i < data.Length && data[i] != null)
|
||||||
|
{
|
||||||
|
node.right = new TreeNode(data[i]!.Value);
|
||||||
|
queue.Enqueue(node.right);
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
static List<int?> FlattenToOutputList(TreeNode? root)
|
||||||
|
{
|
||||||
|
var result = new List<int?>();
|
||||||
|
var node = root;
|
||||||
|
while (node != null)
|
||||||
|
{
|
||||||
|
result.Add(node.val);
|
||||||
|
if (node.right != null)
|
||||||
|
result.Add(null);
|
||||||
|
node = node.right;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static string FormatNullable(int? value) => value?.ToString() ?? "null";
|
||||||
27
114. Flatten Binary Tree to Linked List/Solution.cs
Normal file
27
114. Flatten Binary Tree to Linked List/Solution.cs
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
|
||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public void Flatten(TreeNode? root)
|
||||||
|
{
|
||||||
|
if (root == null)
|
||||||
|
return;
|
||||||
|
var head = root;
|
||||||
|
var stack = new Stack<TreeNode>();
|
||||||
|
if (root.right != null)
|
||||||
|
stack.Push(root.right);
|
||||||
|
if (root.left != null)
|
||||||
|
stack.Push(root.left);
|
||||||
|
while (stack.Count > 0)
|
||||||
|
{
|
||||||
|
var node = stack.Pop();
|
||||||
|
head.left = null;
|
||||||
|
head.right = node;
|
||||||
|
head = node;
|
||||||
|
if (node.right != null)
|
||||||
|
stack.Push(node.right);
|
||||||
|
if (node.left != null)
|
||||||
|
stack.Push(node.left);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
114. Flatten Binary Tree to Linked List/TreeNode.cs
Normal file
12
114. Flatten Binary Tree to Linked List/TreeNode.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
14
Leetcode.sln
14
Leetcode.sln
@@ -193,6 +193,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "31. Next Permutation", "31.
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "32. Longest Valid Parentheses", "32. Longest Valid Parentheses\32. Longest Valid Parentheses.csproj", "{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "32. Longest Valid Parentheses", "32. Longest Valid Parentheses\32. Longest Valid Parentheses.csproj", "{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "114. Flatten Binary Tree to Linked List", "114. Flatten Binary Tree to Linked List\114. Flatten Binary Tree to Linked List.csproj", "{D86CCC06-597D-4DF8-8DCC-E7804E773530}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -1343,6 +1345,18 @@ Global
|
|||||||
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x64.Build.0 = Release|Any CPU
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x86.ActiveCfg = Release|Any CPU
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x86.Build.0 = Release|Any CPU
|
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
Reference in New Issue
Block a user