原题目:https://leetcode-cn.com/problems/balance-a-binary-search-tree/
思路:
获取中序遍历的结果,然后找去中点,此为根节点,依次递归的构造左子树和右子树
代码:
class Solution {
public:
vector<int> inorderSeq;
void getInorder(TreeNode* o) {
if (o->left) getInorder(o->left);
inorderSeq.push_back(o->val);
if (o->right) getInorder(o->right);
}
TreeNode* build(int l, int r) {
int mid = (l + r) >> 1;
TreeNode* o = new TreeNode(inorderSeq[mid]);
if (l <= mid - 1) o->left = build(l, mid - 1);
if (mid + 1 <= r) o->right = build(mid + 1, r);
return o;
}
TreeNode* balanceBST(TreeNode* root) {
getInorder(root);
return build(0, inorderSeq.size() - 1);
}
};