How to Balance a Binary Search Tree using Recursive Inorder Trav
- 时间:2020-09-10 13:03:17
- 分类:网络文摘
- 阅读:119 次
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) —
推荐阅读:奥数题:1994年“世界杯”足球赛中 数学题:如何按照一定的比例把小长方形扩大成与大长方形完全重的图形 问答题:说明学生总数、每辆车载客数、客车数成什么比例 数学题:有一个礼品盒,用彩绳扎成如右图的形状 数学题:客车从甲地到乙地要6小时;货车从乙地到甲地要8小时 数学题:一件商品按成本提高30%,换季又打八折 数学题:前三轮的平均的平均分是94 数学题:财务室会计结账时,发现账面上少了890.1元钱 数学题:一个玻璃瓶内原有盐是水的1/11 数学题:把圆柱平均分成若干份后拼成一个长方体
- 评论列表
-
- 添加评论