-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
166 lines (146 loc) · 3.43 KB
/
Solution.cs
File metadata and controls
166 lines (146 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* 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;
* }
* }
*/
public class Solution
{
public TreeNode BalanceBST(TreeNode root)
{
List<int> values = [];
LNR(root, values);
TreeNode ans = null;
foreach (int val in values)
{
ans = Insert(ans, val);
}
return ans;
}
Dictionary<int, int> height = [];
void LNR(TreeNode root, List<int> values)
{
if (root is null) return;
LNR(root.left, values);
values.Add(root.val);
LNR(root.right, values);
}
void UpdateHeight(TreeNode x)
{
int left = 0, right = 0;
if (x.left is not null)
{
left = height.GetValueOrDefault(x.left.val, 0);
}
if (x.right is not null)
{
right = height.GetValueOrDefault(x.right.val, 0);
}
height[x.val] = 1 + Math.Max(left, right);
}
int Balance(TreeNode x)
{
int left = 0, right = 0;
if (x.left is not null)
{
left = height.GetValueOrDefault(x.left.val, 0);
}
if (x.right is not null)
{
right = height.GetValueOrDefault(x.right.val, 0);
}
return left - right;
}
/*
y x
/ \ / \
x T3 -> T1 y
/ \ / \
T1 T2 T2 T3
*/
TreeNode RotateRight(TreeNode y)
{
TreeNode x = y.left;
TreeNode t2 = x.right;
x.right = y;
y.left = t2;
UpdateHeight(y);
UpdateHeight(x);
return x;
}
/*
x y
/ \ / \
T1 y -> x T3
/ \ / \
T2 T3 T1 T2
*/
TreeNode RotateLeft(TreeNode x)
{
TreeNode y = x.right;
TreeNode t2 = y.left;
y.left = x;
x.right = t2;
UpdateHeight(x);
UpdateHeight(y);
return y;
}
TreeNode ReBalance(TreeNode root)
{
UpdateHeight(root);
int balance = Balance(root);
// left heavy
if (balance > 1)
{
if (Balance(root.left) < 0) // LR case
{
root.left = RotateLeft(root.left);
}
else
{
return RotateRight(root); // LL case
}
}
// right heavy
if (balance < -1)
{
if (Balance(root.right) > 0) // RL case
{
root.right = RotateRight(root.right);
}
else
{
return RotateLeft(root); // RR case
}
}
return root;
}
TreeNode Insert(TreeNode root, int val)
{
if (root is null)
{
height[val] = 1;
return new(val);
}
if (root.val < val)
{
root.right = Insert(root.right, val);
}
else if (root.val > val)
{
root.left = Insert(root.left, val);
}
else
{
return root;
}
return ReBalance(root);
}
}