diff --git a/114. Flatten Binary Tree to Linked List/114. Flatten Binary Tree to Linked List.csproj b/114. Flatten Binary Tree to Linked List/114. Flatten Binary Tree to Linked List.csproj new file mode 100644 index 0000000..6c180df --- /dev/null +++ b/114. Flatten Binary Tree to Linked List/114. Flatten Binary Tree to Linked List.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + _114._Flatten_Binary_Tree_to_Linked_List + enable + enable + + + diff --git a/114. Flatten Binary Tree to Linked List/Program.cs b/114. Flatten Binary Tree to Linked List/Program.cs new file mode 100644 index 0000000..f37029d --- /dev/null +++ b/114. Flatten Binary Tree to Linked List/Program.cs @@ -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(), Array.Empty()), + // (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(); + 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 FlattenToOutputList(TreeNode? root) +{ + var result = new List(); + 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"; diff --git a/114. Flatten Binary Tree to Linked List/Solution.cs b/114. Flatten Binary Tree to Linked List/Solution.cs new file mode 100644 index 0000000..29df168 --- /dev/null +++ b/114. Flatten Binary Tree to Linked List/Solution.cs @@ -0,0 +1,27 @@ + +public class Solution +{ + public void Flatten(TreeNode? root) + { + if (root == null) + return; + var head = root; + var stack = new Stack(); + 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); + + } + } +} \ No newline at end of file diff --git a/114. Flatten Binary Tree to Linked List/TreeNode.cs b/114. Flatten Binary Tree to Linked List/TreeNode.cs new file mode 100644 index 0000000..4a5ea48 --- /dev/null +++ b/114. Flatten Binary Tree to Linked List/TreeNode.cs @@ -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; + } +} diff --git a/Leetcode.sln b/Leetcode.sln index 172e7be..9df1f80 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -193,6 +193,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "31. Next Permutation", "31. 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}" 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 GlobalSection(SolutionConfigurationPlatforms) = preSolution 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|x86.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE