-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1382-Balance_a_Binary_Search_Tree.cpp
More file actions
179 lines (163 loc) · 4.1 KB
/
1382-Balance_a_Binary_Search_Tree.cpp
File metadata and controls
179 lines (163 loc) · 4.1 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
/*******************************************************************************
* 1382-Balance_a_Binary_Search_Tree.cpp
* Billy.Ljm
* 26 June 2024
*
* =======
* Problem
* =======
* https://leetcode.com/problems/balance-a-binary-search-tree/
*
* 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.
*
* ===========
* My Approach
* ===========
* We can traverse through the tree inorder, to extract every element into a
* vector in ascending order. Subsequently, we just have to create a balanced
* binary search tree from that sorted vector.
*
* This has a time complexity of O(n) and space complexity of O(n), where n is
* the number of nodes in the binary tree.
******************************************************************************/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* 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) {}
};
/**
* Create binary tree from elements specified in level-order
*/
TreeNode* BinaryTree(vector<int>& elements) {
queue<TreeNode*> queue;
TreeNode* root = new TreeNode(elements[0]);
queue.push(root);
size_t i = 1;
while (i < elements.size()) {
TreeNode* current = queue.front();
queue.pop();
if (i < elements.size() && elements[i] != -1) {
current->left = new TreeNode(elements[i]);
queue.push(current->left);
}
i++;
if (i < elements.size() && elements[i] != -1) {
current->right = new TreeNode(elements[i]);
queue.push(current->right);
}
i++;
}
return root;
}
/**
* Print binary tree as elements specified in level-order
*/
void printTree(TreeNode* root) {
queue<TreeNode*> queue;
queue.push(root);
cout << "[";
while (!queue.empty()) {
TreeNode* current = queue.front();
queue.pop();
if (current) {
cout << current->val << ",";
queue.push(current->left);
queue.push(current->right);
}
else {
cout << -1 << ",";
}
}
cout << "\b]" << endl;
}
/**
* Delete binary tree
*/
void deleteTree(TreeNode* root) {
if (root == nullptr) return;
deleteTree(root->left);
deleteTree(root->right);
delete root;
}
/**
* Solution
*/
class Solution {
private:
/* in-order traversal of binary tree, and saving elements to given vector */
void inOrder(TreeNode* root, vector<int>& elements) {
if (root == nullptr) return;
inOrder(root->left, elements);
elements.push_back(root->val);
inOrder(root->right, elements);
}
/* creates a balanced binary tree from given elements*/
TreeNode* balancedTree(int idmin, int idmax, vector<int>& elements) {
if (idmin > idmax) return nullptr;
int idmid = (idmin + idmax) / 2;
return new TreeNode(elements[idmid],
balancedTree(idmin, idmid - 1, elements),
balancedTree(idmid + 1, idmax, elements));
}
public:
TreeNode* balanceBST(TreeNode* root) {
vector<int> elements;
inOrder(root, elements);
return balancedTree(0, elements.size() - 1, elements);
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
TreeNode *in, *out;
vector<int> elements;
// test case 1
elements = { 1,-1,2,-1,3,-1,4,-1,-1 };
in = BinaryTree(elements);
out = sol.balanceBST(in);
std::cout << "balanceBST(" << elements << ") = ";
printTree(out);
std::cout << std::endl;
deleteTree(in);
deleteTree(out);
// test case 2
elements = { 2,1,3 };
in = BinaryTree(elements);
out = sol.balanceBST(in);
std::cout << "balanceBST(" << elements << ") = ";
printTree(out);
std::cout << std::endl;
deleteTree(in);
deleteTree(out);
return 0;
}