Skip to content

Commit d4408e4

Browse files
author
Bogdan Hladiuc
committed
Day 8 Binary Tree To Double Linked List Q426
1 parent 701027b commit d4408e4

File tree

10 files changed

+299
-8
lines changed

10 files changed

+299
-8
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\..\Library\Library.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<TargetFramework>net6.0</TargetFramework>
9+
<ImplicitUsings>enable</ImplicitUsings>
10+
<Nullable>enable</Nullable>
11+
</PropertyGroup>
12+
13+
</Project>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#nullable disable warnings
2+
using Library;
3+
4+
namespace Formation {
5+
6+
public class TreeToDoubleLinkedList {
7+
public static void Run() {
8+
var root = new TreeNode(1, null, null);
9+
root.Left = new TreeNode(2, null, null);
10+
root.Right = new TreeNode(3, null, null);
11+
12+
root.Left.Left = new TreeNode(4, null, null);
13+
root.Left.Right = new TreeNode(5, null, null);
14+
15+
root.Right.Left = new TreeNode(6, null, null);
16+
root.Right.Right = new TreeNode(7, null, null);
17+
18+
// root.Print();
19+
20+
var result = Solution(root);
21+
22+
// result.Print();
23+
}
24+
25+
private static TreeNode Solution(TreeNode root) {
26+
if (root == null) {
27+
return root;
28+
}
29+
30+
TreeNode first = null;
31+
TreeNode last = null;
32+
var stack = new Stack<TreeNode>();
33+
34+
while (root != null || stack.Count > 0) {
35+
while (root != null) {
36+
stack.Push(root);
37+
root = root.Left;
38+
}
39+
40+
root = stack.Pop();
41+
42+
if (first == null) {
43+
first = root;
44+
}
45+
46+
if (last != null) {
47+
last.Right = root;
48+
root.Left = last;
49+
}
50+
51+
last = root;
52+
root = root.Right;
53+
}
54+
55+
last.Right = first;
56+
first.Left = last;
57+
58+
return first;
59+
}
60+
}
61+
}

LeetcodeNew.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_6_Search_In_Matrix_Q240
9999
EndProject
100100
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_7_Palindrome_Anagram", "Formation\21_Days_Challenge\Day_7_Palindrome_Anagram\Day_7_Palindrome_Anagram.csproj", "{F4A8CB4B-D714-46CB-8D5E-B7585A9A969A}"
101101
EndProject
102+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_8_TreeToList_Q426", "Formation\21_Days_Challenge\Day_8_TreeToList_Q426\Day_8_TreeToList_Q426.csproj", "{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}"
103+
EndProject
102104
Global
103105
GlobalSection(SolutionConfigurationPlatforms) = preSolution
104106
Debug|Any CPU = Debug|Any CPU
@@ -664,6 +666,18 @@ Global
664666
{F4A8CB4B-D714-46CB-8D5E-B7585A9A969A}.Release|x64.Build.0 = Release|Any CPU
665667
{F4A8CB4B-D714-46CB-8D5E-B7585A9A969A}.Release|x86.ActiveCfg = Release|Any CPU
666668
{F4A8CB4B-D714-46CB-8D5E-B7585A9A969A}.Release|x86.Build.0 = Release|Any CPU
669+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
670+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Debug|Any CPU.Build.0 = Debug|Any CPU
671+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Debug|x64.ActiveCfg = Debug|Any CPU
672+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Debug|x64.Build.0 = Debug|Any CPU
673+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Debug|x86.ActiveCfg = Debug|Any CPU
674+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Debug|x86.Build.0 = Debug|Any CPU
675+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Release|Any CPU.ActiveCfg = Release|Any CPU
676+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Release|Any CPU.Build.0 = Release|Any CPU
677+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Release|x64.ActiveCfg = Release|Any CPU
678+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Release|x64.Build.0 = Release|Any CPU
679+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Release|x86.ActiveCfg = Release|Any CPU
680+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59}.Release|x86.Build.0 = Release|Any CPU
667681
EndGlobalSection
668682
GlobalSection(NestedProjects) = preSolution
669683
{45EB27A3-9FBB-48ED-8C89-E0F074C02E8C} = {71C7D88B-6D5D-4E25-A275-869603700831}
@@ -674,5 +688,6 @@ Global
674688
{544EBBCF-F78C-46C0-8C1B-E3D4760ECD18} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
675689
{D932B4EA-845D-48B3-ADFD-C9EBD3E6232A} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
676690
{F4A8CB4B-D714-46CB-8D5E-B7585A9A969A} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
691+
{6A1132F8-3D68-468D-AEB9-4B965EC7CC59} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
677692
EndGlobalSection
678693
EndGlobal

Library/BTreePrinter.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#nullable disable warnings
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace Library {
6+
public static class BTreePrinter {
7+
class NodeInfo {
8+
public TreeNode Node;
9+
public string Text;
10+
public int StartPos;
11+
public int Size { get { return Text.Length; } }
12+
public int EndPos { get { return StartPos + Size; } set { StartPos = value - Size; } }
13+
public NodeInfo Parent, Left, Right;
14+
}
15+
16+
public static void Print(this TreeNode root, string textFormat = "0", int spacing = 1, int topMargin = 2, int leftMargin = 2) {
17+
if (root == null) return;
18+
int rootTop = Console.CursorTop + topMargin;
19+
var last = new List<NodeInfo>();
20+
var next = root;
21+
for (int level = 0; next != null; level++) {
22+
var item = new NodeInfo { Node = next, Text = next.Val.ToString(textFormat) };
23+
if (level < last.Count) {
24+
item.StartPos = last[level].EndPos + spacing;
25+
last[level] = item;
26+
} else {
27+
item.StartPos = leftMargin;
28+
last.Add(item);
29+
}
30+
if (level > 0) {
31+
item.Parent = last[level - 1];
32+
if (next == item.Parent.Node.Left) {
33+
item.Parent.Left = item;
34+
item.EndPos = Math.Max(item.EndPos, item.Parent.StartPos - 1);
35+
} else {
36+
item.Parent.Right = item;
37+
item.StartPos = Math.Max(item.StartPos, item.Parent.EndPos + 1);
38+
}
39+
}
40+
next = next.Left ?? next.Right;
41+
for (; next == null; item = item.Parent) {
42+
int top = rootTop + 2 * level;
43+
Print(item.Text, top, item.StartPos);
44+
if (item.Left != null) {
45+
Print("/", top + 1, item.Left.EndPos);
46+
Print("_", top, item.Left.EndPos + 1, item.StartPos);
47+
}
48+
if (item.Right != null) {
49+
Print("_", top, item.EndPos, item.Right.StartPos - 1);
50+
Print("\\", top + 1, item.Right.StartPos - 1);
51+
}
52+
if (--level < 0) break;
53+
if (item == item.Parent.Left) {
54+
item.Parent.StartPos = item.EndPos + 1;
55+
next = item.Parent.Node.Right;
56+
} else {
57+
if (item.Parent.Left == null)
58+
item.Parent.EndPos = item.StartPos - 1;
59+
else
60+
item.Parent.StartPos += (item.StartPos - 1 - item.Parent.EndPos) / 2;
61+
}
62+
}
63+
}
64+
Console.SetCursorPosition(0, rootTop + 2 * last.Count - 1);
65+
}
66+
67+
private static void Print(string s, int top, int left, int right = -1) {
68+
Console.SetCursorPosition(left, top);
69+
if (right < 0) right = left + s.Length;
70+
while (Console.CursorLeft < right) Console.Write(s);
71+
}
72+
}
73+
}

Library/DoubleLinkedListNode.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Diagnostics;
2+
using System.Text;
3+
4+
namespace Library {
5+
6+
[DebuggerDisplay("{Val}")]
7+
public class DoubleLinkedListNode {
8+
public int Val { get; set; }
9+
public DoubleLinkedListNode? Next { get; set; }
10+
public DoubleLinkedListNode? Prev { get; set; }
11+
12+
public DoubleLinkedListNode() {
13+
Val = 0;
14+
Next = null;
15+
Prev = null;
16+
}
17+
18+
public DoubleLinkedListNode(int val, DoubleLinkedListNode next, DoubleLinkedListNode prev) {
19+
Val = val;
20+
Next = next;
21+
Prev = prev;
22+
}
23+
24+
public void Print() {
25+
var sb = new StringBuilder();
26+
27+
var curr = this;
28+
29+
if (curr != null) {
30+
sb.Append($"{curr.Val} ⇆ ");
31+
curr = curr.Next;
32+
}
33+
34+
while (curr != this && curr != null) {
35+
sb.Append($"{curr.Val} ⇆ ");
36+
curr = curr.Next;
37+
}
38+
39+
if (sb.Length > 0) {
40+
sb.Length = sb.Length - 3;
41+
}
42+
43+
Console.WriteLine(sb.ToString());
44+
}
45+
}
46+
}

Run/Program.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ static void Main(string[] args) {
44

55
#region Formation
66

7-
// Formation.DeleteMiddleNode.Run(); // Day 1
8-
// Formation.SpiralTraversal.Run(); // Day 2
9-
// Formation.ReverseKGroup.Run(); // Day 3
10-
// Formation.ToeplitzMatrix.Run(); // Day 4
11-
// Formation.MonotonicMatrix.Run(); // Day 5
12-
// Formation.SearchInMatrix.Run(); // Day 6
13-
Formation.PalindromeAnagram.Run(); // Day 7
7+
// Formation.DeleteMiddleNode.Run(); // Day 1
8+
// Formation.SpiralTraversal.Run(); // Day 2
9+
// Formation.ReverseKGroup.Run(); // Day 3
10+
// Formation.ToeplitzMatrix.Run(); // Day 4
11+
// Formation.MonotonicMatrix.Run(); // Day 5
12+
// Formation.SearchInMatrix.Run(); // Day 6
13+
// Formation.PalindromeAnagram.Run(); // Day 7
14+
Formation.TreeToDoubleLinkedList.Run(); // Day 8
1415

1516
#endregion Formation
1617

Run/Run.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_5_Monotonic_Matrix\Day_5_Monotonic_Matrix.csproj" />
4848
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_6_Search_In_Matrix_Q240\Day_6_Search_In_Matrix_Q240.csproj" />
4949
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_7_Palindrome_Anagram\Day_7_Palindrome_Anagram.csproj" />
50+
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_8_TreeToList_Q426\Day_8_TreeToList_Q426.csproj" />
5051
</ItemGroup>
5152

5253
<PropertyGroup>

Run/obj/Run.csproj.nuget.dgspec.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,62 @@
452452
}
453453
}
454454
},
455+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_8_TreeToList_Q426/Day_8_TreeToList_Q426.csproj": {
456+
"version": "1.0.0",
457+
"restore": {
458+
"projectUniqueName": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_8_TreeToList_Q426/Day_8_TreeToList_Q426.csproj",
459+
"projectName": "Day_8_TreeToList_Q426",
460+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_8_TreeToList_Q426/Day_8_TreeToList_Q426.csproj",
461+
"packagesPath": "/Users/Bogdan/.nuget/packages/",
462+
"outputPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_8_TreeToList_Q426/obj/",
463+
"projectStyle": "PackageReference",
464+
"configFilePaths": [
465+
"/Users/Bogdan/.nuget/NuGet/NuGet.Config"
466+
],
467+
"originalTargetFrameworks": [
468+
"net6.0"
469+
],
470+
"sources": {
471+
"https://api.nuget.org/v3/index.json": {}
472+
},
473+
"frameworks": {
474+
"net6.0": {
475+
"targetAlias": "net6.0",
476+
"projectReferences": {
477+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
478+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj"
479+
}
480+
}
481+
}
482+
},
483+
"warningProperties": {
484+
"warnAsError": [
485+
"NU1605"
486+
]
487+
}
488+
},
489+
"frameworks": {
490+
"net6.0": {
491+
"targetAlias": "net6.0",
492+
"imports": [
493+
"net461",
494+
"net462",
495+
"net47",
496+
"net471",
497+
"net472",
498+
"net48"
499+
],
500+
"assetTargetFallback": true,
501+
"warn": true,
502+
"frameworkReferences": {
503+
"Microsoft.NETCore.App": {
504+
"privateAssets": "all"
505+
}
506+
},
507+
"runtimeIdentifierGraphPath": "/usr/local/share/dotnet/sdk/6.0.202/RuntimeIdentifierGraph.json"
508+
}
509+
}
510+
},
455511
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
456512
"version": "1.0.0",
457513
"restore": {
@@ -2558,6 +2614,9 @@
25582614
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_7_Palindrome_Anagram/Day_7_Palindrome_Anagram.csproj": {
25592615
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_7_Palindrome_Anagram/Day_7_Palindrome_Anagram.csproj"
25602616
},
2617+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_8_TreeToList_Q426/Day_8_TreeToList_Q426.csproj": {
2618+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_8_TreeToList_Q426/Day_8_TreeToList_Q426.csproj"
2619+
},
25612620
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
25622621
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj"
25632622
},

Run/obj/project.assets.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,19 @@
106106
"bin/placeholder/Day_7_Palindrome_Anagram.dll": {}
107107
}
108108
},
109+
"Day_8_TreeToList_Q426/1.0.0": {
110+
"type": "project",
111+
"framework": ".NETCoreApp,Version=v6.0",
112+
"dependencies": {
113+
"Library": "1.0.0"
114+
},
115+
"compile": {
116+
"bin/placeholder/Day_8_TreeToList_Q426.dll": {}
117+
},
118+
"runtime": {
119+
"bin/placeholder/Day_8_TreeToList_Q426.dll": {}
120+
}
121+
},
109122
"Library/1.0.0": {
110123
"type": "project",
111124
"framework": ".NETCoreApp,Version=v6.0",
@@ -592,6 +605,11 @@
592605
"path": "../Formation/21_Days_Challenge/Day_7_Palindrome_Anagram/Day_7_Palindrome_Anagram.csproj",
593606
"msbuildProject": "../Formation/21_Days_Challenge/Day_7_Palindrome_Anagram/Day_7_Palindrome_Anagram.csproj"
594607
},
608+
"Day_8_TreeToList_Q426/1.0.0": {
609+
"type": "project",
610+
"path": "../Formation/21_Days_Challenge/Day_8_TreeToList_Q426/Day_8_TreeToList_Q426.csproj",
611+
"msbuildProject": "../Formation/21_Days_Challenge/Day_8_TreeToList_Q426/Day_8_TreeToList_Q426.csproj"
612+
},
595613
"Library/1.0.0": {
596614
"type": "project",
597615
"path": "../Library/Library.csproj",
@@ -793,6 +811,7 @@
793811
"Day_5_Monotonic_Matrix >= 1.0.0",
794812
"Day_6_Search_In_Matrix_Q240 >= 1.0.0",
795813
"Day_7_Palindrome_Anagram >= 1.0.0",
814+
"Day_8_TreeToList_Q426 >= 1.0.0",
796815
"Library >= 1.0.0",
797816
"Q10_Regular_Expression_Matching >= 1.0.0",
798817
"Q1249_Minimum_Remove_To_Make_Valid_Parentheses >= 1.0.0",
@@ -882,6 +901,9 @@
882901
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_7_Palindrome_Anagram/Day_7_Palindrome_Anagram.csproj": {
883902
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_7_Palindrome_Anagram/Day_7_Palindrome_Anagram.csproj"
884903
},
904+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_8_TreeToList_Q426/Day_8_TreeToList_Q426.csproj": {
905+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_8_TreeToList_Q426/Day_8_TreeToList_Q426.csproj"
906+
},
885907
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
886908
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj"
887909
},

Run/obj/project.nuget.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": 2,
3-
"dgSpecHash": "Q1UoYUeRvCP5ZvnSn0DqVdmGQMhA98wsHDw/iy5E/hCJuZcnsMM7+RjBng6PqtYVVmo/0KSRq9cg1nETw81X9w==",
3+
"dgSpecHash": "2jy+JlJRfwBelnf1oxP56BylVuDJ4DP5ndq9gpcf/Wujd3tcPkpXEZk+Kf8GucgoB8/aqCRpbG2T2cFfdZyK6A==",
44
"success": true,
55
"projectFilePath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Run/Run.csproj",
66
"expectedPackageFiles": [],

0 commit comments

Comments
 (0)