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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user