How to Balance a Binary Search Tree using Recursive Inorder Trav
- Time:2020-09-10 13:03:17
- Class:Weblog
- Read:30
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.
- Recursive Algorithm to Construct Binary Tree from Preorder and Postorder Traversal
- How to Construct Binary Search Tree from Preorder Traversal in Python?
- Algorithm to Construct Binary Tree from Preorder and Inorder Traversal
- How to Construct Binary Search Tree from Preorder Traversal? (C++ and Java)
- How to Construct String from Binary Tree?
- How to Balance a Binary Search Tree using Recursive Inorder Traversal Algorithm?
- How to Construct the Maximum Binary Tree using Divide-and-Conquer Recursion Algorithm?
- How to Construct Binary Tree from Inorder and Postorder Traversal using Depth First Search Algorithm (Recursion)?
- How to Construct Binary Tree from String (Binary Tree Deserialization Algorithm)
–EOF (The Ultimate Computing & Technology Blog) —
Recommend:Switching to a Blogging Career: Can You Afford to Work from Home
4 Features that Will Enhance Your Blog
Amazon Surpasses Google: Should you Change your SEO Strategies?
How to Optimize WordPress Categories and Tags
Blogging Royalties: Michelle Obama Interviewing Barack on her Po
Content Marketing: Expectations Vs Reality
Blogger Skills: LinkedIn and Microsoft’s Digital Skill Programs
5 Tips for Creating a Content Strategy for Your eCommerce Websit
5 Tips You Can Use to Launch a Successful Property Management Bl
Blogging and Gaming Finally Recognized as Professions
- Comment list
-
- Comment add