版权声明:文章都是原创,转载请注明~~~~ https://blog.csdn.net/SourDumplings/article/details/88179420
Easy
97699FavoriteShare
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
Given the sorted array: [-10,-3,0,5,9], One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: 0 / \ -3 9 / / -10 5
C++:
二分法递归建静态建BST:
/*
* @Author: SourDumplings
* @Link: https://github.com/SourDumplings/
* @Email: [email protected]
* @Description: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
* @Date: 2019-03-05 13:32:32
*/
/**
* 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 *sortedArrayToBST(vector<int> &nums)
{
if (nums.empty())
{
return nullptr;
}
int n = nums.size();
int mid = n / 2;
TreeNode *root = new TreeNode(nums[mid]);
vector<int> left, right;
for (int i = 0; i < mid; i++)
{
left.push_back(nums[i]);
}
for (int i = mid + 1; i < n; i++)
{
right.push_back(nums[i]);
}
root->left = sortedArrayToBST(left);
root->right = sortedArrayToBST(right);
return root;
}
};
动态建AVL树-3+4重构法:
/*
* @Author: SourDumplings
* @Link: https://github.com/SourDumplings/
* @Email: [email protected]
* @Description: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
* @Date: 2019-03-05 12:24:07
*/
/**
* 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 *sortedArrayToBST(vector<int> &nums)
{
TreeNode *root = nullptr;
for (int num : nums)
{
insert(root, num);
}
return root;
}
void *insert(TreeNode *&root, int num)
{
if (root == nullptr)
{
root = new TreeNode(num);
}
else
{
if (num < root->val)
{
insert(root->left, num);
if (get_height(root->left) - get_height(root->right) >= 2)
{
if (num < root->left->val)
{
root = reconstruct34(root->left->left, root->left, root,
root->left->left->left, root->left->left->right,
root->left->right, root->right);
}
else
{
root = reconstruct34(root->left, root->left->right, root,
root->left->left, root->left->right->left,
root->left->right->right, root->right);
}
}
}
if (num > root->val)
{
insert(root->right, num);
if (get_height(root->left) - get_height(root->right) <= -2)
{
if (num > root->right->val)
{
root = reconstruct34(root, root->right, root->right->right,
root->left, root->right->left, root->right->right->left,
root->right->right->right);
}
else
{
root = reconstruct34(root, root->right->left, root->right,
root->left, root->right->left->left, root->right->left->right,
root->right->right);
}
}
}
}
return root;
}
TreeNode *reconstruct34(TreeNode *A, TreeNode *B, TreeNode *C,
TreeNode *T1, TreeNode *T2, TreeNode *T3, TreeNode *T4)
{
B->left = A;
B->right = C;
A->left = T1;
A->right = T2;
C->left = T3;
C->right = T4;
return B;
}
int get_height(TreeNode *root)
{
if (root == nullptr)
{
return 0;
}
else
{
return max(get_height(root->left), get_height(root->right)) + 1;
}
}
};
动态建AVL树-左右旋法:
/*
* @Author: SourDumplings
* @Link: https://github.com/SourDumplings/
* @Email: [email protected]
* @Description: https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
* @Date: 2019-03-05 11:56:35
*/
/**
* 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 *sortedArrayToBST(vector<int> &nums)
{
TreeNode *root = nullptr;
for (int num : nums)
{
insert(root, num);
}
return root;
}
void *insert(TreeNode *&root, int num)
{
if (root == nullptr)
{
root = new TreeNode(num);
}
else
{
if (num < root->val)
{
insert(root->left, num);
if (get_height(root->left) - get_height(root->right) >= 2)
{
if (num < root->left->val)
{
single_left_rotation(root);
}
else
{
double_left_right_rotation(root);
}
}
}
if (num > root->val)
{
insert(root->right, num);
if (get_height(root->left) - get_height(root->right) <= -2)
{
if (num > root->right->val)
{
single_right_rotation(root);
}
else
{
double_right_left_rotation(root);
}
}
}
}
return root;
}
void single_left_rotation(TreeNode *&A)
{
TreeNode *B = A->left;
A->left = B->left;
B->right = A;
A = B;
}
void single_right_rotation(TreeNode *&A)
{
TreeNode *B = A->right;
A->right = B->left;
B->left = A;
A = B;
}
void double_left_right_rotation(TreeNode *&A)
{
single_right_rotation(A->left);
single_left_rotation(A);
}
void double_right_left_rotation(TreeNode *&A)
{
single_left_rotation(A->left);
single_right_rotation(A);
}
int get_height(TreeNode *root)
{
if (root == nullptr)
{
return 0;
}
else
{
return max(get_height(root->left), get_height(root->right)) + 1;
}
}
};