forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 14
/
balance-a-binary-search-tree.cpp
90 lines (83 loc) · 2.48 KB
/
balance-a-binary-search-tree.cpp
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
// Time: O(n)
// Space: O(h)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
// dfs solution with stack
class Solution {
public:
TreeNode* balanceBST(TreeNode* root) {
return sortedArrayToBst(inorderTraversal(root));
}
private:
TreeNode *sortedArrayToBst(const vector<int>& arr) {
TreeNode *result = nullptr;
vector<tuple<int, int, TreeNode**>> stk = {{0, arr.size(), &result}};
while (!stk.empty()) {
const auto [i, j, ret] = stk.back(); stk.pop_back();
if (i >= j) {
continue;
}
const auto& mid = i + (j - i) / 2;
*ret = new TreeNode(arr[mid]);
stk.emplace_back(mid + 1, j, &((*ret)->right));
stk.emplace_back(i, mid, &((*ret)->left));
}
return result;
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
vector<pair<TreeNode *, bool>> stk;
stk.emplace_back(root, false);
while (!stk.empty()) {
const auto [node, visited] = stk.back(); stk.pop_back();
if (!node) {
continue;
}
if (visited) {
result.emplace_back(node->val);
} else {
stk.emplace_back(node->right, false);
stk.emplace_back(node, true);
stk.emplace_back(node->left, false);
}
}
return result;
}
};
// Time: O(n)
// Space: O(h)
// dfs solution with recursion
class Solution2 {
public:
TreeNode* balanceBST(TreeNode* root) {
vector<int> arr;
inorderTraversalHelper(root, &arr);
return sortedArrayToBstHelper(arr, 0, arr.size());
}
private:
TreeNode *sortedArrayToBstHelper(const vector<int>& arr, int i, int j) {
if (i >= j) {
return nullptr;
}
const auto& mid = i + (j - i) / 2;
auto result = new TreeNode(arr[mid]);
result->left = sortedArrayToBstHelper(arr, i, mid);
result->right = sortedArrayToBstHelper(arr, mid + 1, j);
return result;
}
void inorderTraversalHelper(TreeNode* root, vector<int> *arr) {
if (!root) {
return;
}
inorderTraversalHelper(root->left, arr);
arr->emplace_back(root->val);
inorderTraversalHelper(root->right, arr);
}
};