Formatted question description: https://leetcode.ca/all/1382.html

1382. Balance a Binary Search Tree (Medium)

Given a binary search tree, return a balanced binary search tree with the same node values.

A binary search tree is balanced if and only if the depth of the two subtrees of every node never differ by more than 1.

If there is more than one answer, return any of them.

 

Example 1:

Input: root = [1,null,2,null,3,null,4,null,null]
Output: [2,1,3,null,null,null,4]
Explanation: This is not the only correct answer, [3,1,4,null,2,null,null] is also correct.

 

Constraints:

  • The number of nodes in the tree is between 1 and 10^4.
  • The tree nodes will have distinct values between 1 and 10^5.

Related Topics:
Binary Search Tree

Solution 1.

// OJ: https://leetcode.com/problems/balance-a-binary-search-tree/

// Time: O(H)
// Space: O(N)
class Solution {
    vector<int> v;
    void inorder(TreeNode *root) {
        if (!root) return;
        inorder(root->left);
        v.push_back(root->val);
        inorder(root->right);
    }
    TreeNode *build(int start, int end) {
        if (start >= end) return NULL;
        int mid = (start + end) / 2;
        auto node = new TreeNode(v[mid]);
        node->left = build(start, mid);
        node->right = build(mid + 1, end);
        return node;
    }
public:
    TreeNode* balanceBST(TreeNode* root) {
        inorder(root);
        return build(0, v.size());
    }
};

Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode balanceBST(TreeNode root) {
        if (root == null || root.left == null && root.right == null)
            return root;
        List<TreeNode> inorderTraversal = inorderTraversal(root);
        return balanceBST(inorderTraversal);
    }

    public List<TreeNode> inorderTraversal(TreeNode root) {
        List<TreeNode> inorderTraversal = new ArrayList<TreeNode>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode node = root;
        while (!stack.isEmpty() || node != null) {
            while (node != null) {
                stack.push(node);
                node = node.left;
            }
            TreeNode visitNode = stack.pop();
            inorderTraversal.add(visitNode);
            node = visitNode.right;
        }
        return inorderTraversal;
    }

    public TreeNode balanceBST(List<TreeNode> inorderTraversal) {
        int size = inorderTraversal.size();
        if (size == 1) {
            TreeNode root = new TreeNode(inorderTraversal.get(0).val);
            return root;
        } else if (size == 2) {
            TreeNode root = new TreeNode(inorderTraversal.get(1).val);
            TreeNode leftChild = new TreeNode(inorderTraversal.get(0).val);
            root.left = leftChild;
            return root;
        } else if (size == 3) {
            TreeNode root = new TreeNode(inorderTraversal.get(1).val);
            TreeNode leftChild = new TreeNode(inorderTraversal.get(0).val);
            TreeNode rightChild = new TreeNode(inorderTraversal.get(2).val);
            root.left = leftChild;
            root.right = rightChild;
            return root;
        } else {
            int rootIndex = (size - 1) / 2;
            TreeNode root = new TreeNode(inorderTraversal.get(rootIndex).val);
            List<TreeNode> leftInorderTraversal = new ArrayList<TreeNode>();
            List<TreeNode> rightInorderTraversal = new ArrayList<TreeNode>();
            for (int i = 0; i < rootIndex; i++)
                leftInorderTraversal.add(inorderTraversal.get(i));
            for (int i = rootIndex + 1; i < size; i++)
                rightInorderTraversal.add(inorderTraversal.get(i));
            root.left = balanceBST(leftInorderTraversal);
            root.right = balanceBST(rightInorderTraversal);
            return root;
        }
    }
}

All Problems

All Solutions