How to Balance a Binary Search Tree using Recursive Inorder Traversal Algorithm?


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.

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.

Hints:
Convert the tree to a sorted array using an in-order traversal.
Construct a new balanced tree from the sorted array recursively.

Transform to a Balance Binary Search Tree

Given the tree is already a Binary Search Tree (BST), we can use the inorder traversal algorithm (in recursion) to convert the BST to a sorted array.

Then, we can recursively build a balance binary search tree (BBST) by selecting the middle of the array as a root, then spliting into two sub binary trees.

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
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* balanceBST(TreeNode* root) {
        if (!root) return NULL;
        vector<int> arr = inOrder(root);
        return convert(arr, 0, arr.size() - 1);
    }
    
private:
    vector<int> inOrder(TreeNode* root) {
        if (!root) return {};
        vector<int> r;
        vector<int> left = inOrder(root->left);
        if (!left.empty()) {
            r.insert(end(r), begin(left), end(left));
        }
        r.push_back(root->val);
        vector<int> right = inOrder(root->right);
        if (!right.empty()) {
            r.insert(end(r), begin(right), end(right));
        }
        return r;
    }
    
    TreeNode* convert(vector<int> &arr, int left, int right) {
        if (left > right) return NULL;
        if (left == right) return new TreeNode(arr[left]);
        int mid = (left + right) / 2;
        TreeNode* root = new TreeNode(arr[mid]);
        root->left = convert(arr, left, mid - 1);
        root->right = convert(arr, mid + 1, right);
        return root;
    }
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* balanceBST(TreeNode* root) {
        if (!root) return NULL;
        vector<int> arr = inOrder(root);
        return convert(arr, 0, arr.size() - 1);
    }
    
private:
    vector<int> inOrder(TreeNode* root) {
        if (!root) return {};
        vector<int> r;
        vector<int> left = inOrder(root->left);
        if (!left.empty()) {
            r.insert(end(r), begin(left), end(left));
        }
        r.push_back(root->val);
        vector<int> right = inOrder(root->right);
        if (!right.empty()) {
            r.insert(end(r), begin(right), end(right));
        }
        return r;
    }
    
    TreeNode* convert(vector<int> &arr, int left, int right) {
        if (left > right) return NULL;
        if (left == right) return new TreeNode(arr[left]);
        int mid = (left + right) / 2;
        TreeNode* root = new TreeNode(arr[mid]);
        root->left = convert(arr, left, mid - 1);
        root->right = convert(arr, mid + 1, right);
        return root;
    }
};

To convert a BST into a sorted array, it takes O(N) time and O(N) space. Then converting a sorted array back to a Balanced Binary Search Tree also takes O(N) time and O(N) space.

Both procedures are implemented using Recursion which is concise and straight to the point.

Related Binary Tree Construction Algorithms

You may also like the following posts on the similar tree problems.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
795 words
Last Post: Finding the Lucky Numbers in a Matrix
Next Post: Applicable Accounting Software For Churches

The Permanent URL is: How to Balance a Binary Search Tree using Recursive Inorder Traversal Algorithm?

Leave a Reply