-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolution.cpp
59 lines (52 loc) · 1.3 KB
/
Solution.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
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <tuple>
#include <deque>
#include <unordered_map>
#include <cmath>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
vector<int> n ;
void into_vec(TreeNode * root) {
if (root == NULL) return;
into_vec(root->left);
n.push_back(root->val);
into_vec(root->right);
}
TreeNode * maketree(int l, int r) {
// l inclusive, r exclusive
if (l + 1 > r) return NULL;
if (l + 1 == r) return new TreeNode(n[l]);
if (l + 2 == r) {
TreeNode * head = new TreeNode (n[l]);
TreeNode * child = new TreeNode(n[l+1]);
head->right = child;
return head;
}
int m = (l+r)/2;
TreeNode * left = maketree(l,m);
TreeNode * right = maketree(m+1,r);
TreeNode * head = new TreeNode( n[m]);
head->left = left;
head->right = right;
return head;
}
TreeNode* balanceBST(TreeNode* root) {
into_vec(root);
return maketree(0, n.size());
}
};
int main() {
Solution s;
return 0;
}