-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_1382.cpp
More file actions
186 lines (156 loc) · 5.34 KB
/
Leetcode_1382.cpp
File metadata and controls
186 lines (156 loc) · 5.34 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// 1382. Balance a Binary Search Tree
/*
TIME COMPLEXITY: O(n) where n is the number of nodes in the tree
- Morris traversal takes O(n) time to create a sorted array
- Building a balanced BST from sorted array takes O(n) time
- Overall time complexity is O(n)
SPACE COMPLEXITY: O(n)
- We store all node pointers in a vector
- Morris traversal uses O(1) extra space, but the vector requires O(n) space
*/
/*
Given the root of a binary search tree, return a balanced binary search tree with the same node values.
If there is more than one answer, return any of them.
A binary search tree is balanced if the depth of the two subtrees of every node never differs by more than 1.
Example 1:
Input: root = [1,null,2,null,3,null,4,null,null]
Output: [2,1,3,null,null,null,4]
Explanation: This is not the only correct answer, [3,1,4,null,2] is also correct.
Example 2:
Input: root = [2,1,3]
Output: [2,1,3]
Constraints:
- The number of nodes in the tree is in the range [1, 10^4].
- 1 <= Node.val <= 10^5
*/
#include <iostream>
#include <vector>
using namespace std;
// Definition for a binary tree node.
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
TreeNode* buildBalanced(vector<TreeNode*>& nodes, int start, int end) {
if (start > end) return nullptr;
int mid = start + (end - start) / 2;
TreeNode* root = nodes[mid];
root->left = buildBalanced(nodes, start, mid - 1);
root->right = buildBalanced(nodes, mid + 1, end);
return root;
}
TreeNode* balanceBST(TreeNode* root) {
vector<TreeNode*> nodes;
// Morris inorder traversal to get sorted nodes
TreeNode* curr = root;
while (curr) {
if (!curr->left) {
nodes.push_back(curr);
curr = curr->right;
} else {
TreeNode* temp = curr->left;
while (temp->right && temp->right != curr) {
temp = temp->right;
}
if (temp->right == curr) {
temp->right = nullptr;
nodes.push_back(curr);
curr = curr->right;
} else {
temp->right = curr;
curr = curr->left;
}
}
}
return buildBalanced(nodes, 0, nodes.size() - 1);
}
};
// Helper function to create a binary tree from an array (for testing)
TreeNode* createTree(const vector<int>& values, int index = 0) {
if (index >= values.size() || values[index] == -1) {
return nullptr;
}
TreeNode* root = new TreeNode(values[index]);
root->left = createTree(values, 2 * index + 1);
root->right = createTree(values, 2 * index + 2);
return root;
}
// Helper function to delete the tree and free memory
void deleteTree(TreeNode* root) {
if (!root) return;
deleteTree(root->left);
deleteTree(root->right);
delete root;
}
// Helper function to print tree in level order (for testing)
void printTree(TreeNode* root) {
if (!root) return;
vector<TreeNode*> current, next;
current.push_back(root);
while (!current.empty()) {
for (TreeNode* node : current) {
if (node) {
cout << node->val << " ";
next.push_back(node->left);
next.push_back(node->right);
} else {
cout << "null ";
}
}
cout << endl;
current.clear();
swap(current, next);
}
}
// Test function
void runTests() {
Solution solution;
// Test case 1: Skewed tree
{
cout << "Test Case 1: Skewed tree\n";
vector<int> values = {1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4};
TreeNode* root = createTree(values);
cout << "Original tree (level order):\n";
printTree(root);
TreeNode* balanced = solution.balanceBST(root);
cout << "\nBalanced tree (level order):\n";
printTree(balanced);
deleteTree(balanced); // Clean up
cout << "----------------------------------------\n";
}
// Test case 2: Already balanced tree
{
cout << "Test Case 2: Already balanced tree\n";
vector<int> values = {2, 1, 3};
TreeNode* root = createTree(values);
cout << "Original tree (level order):\n";
printTree(root);
TreeNode* balanced = solution.balanceBST(root);
cout << "\nBalanced tree (level order):\n";
printTree(balanced);
deleteTree(balanced); // Clean up
cout << "----------------------------------------\n";
}
// Test case 3: Single node
{
cout << "Test Case 3: Single node\n";
vector<int> values = {1};
TreeNode* root = createTree(values);
cout << "Original tree (level order):\n";
printTree(root);
TreeNode* balanced = solution.balanceBST(root);
cout << "\nBalanced tree (level order):\n";
printTree(balanced);
deleteTree(balanced); // Clean up
}
}
int main() {
runTests();
return 0;
}