1382. Balance a Binary Search Tree

problem

solution

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
/**
* 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:
void preorder(TreeNode*root, vector<int> & nums){
if(!root) return;

preorder(root->left, nums);
nums.push_back(root->val);
preorder(root->right, nums);
}
TreeNode *build(vector<int> & nums, int l, int r){
if(l>r) return nullptr;

int mid = l+(r-l)/2;
TreeNode *root = new TreeNode(nums[mid]);
root->left = build(nums, l, mid-1);
root->right = build(nums, mid+1, r);
return root;
}
TreeNode* balanceBST(TreeNode* root) {
vector<int> vec;
preorder(root, vec);
return build(vec, 0, vec.size()-1);
}
};

analysis

  • time complexity O(n)
  • space complexity O(n)