有一个增序排列的数组,将其转换为平衡二叉搜索树。
class Node
{
public:
Node(int v)
{
value = v;
left = NULL;
right = NULL;
}
private:
int value;
Node *left;
Node *right;
};
Node* sortedArrayToBST(int a[], int left, int right)
{
if (left > right)
{
return NULL;
}
int mid = left + (right-left)/2;
Node *root = new Node(a[mid]);
root->left = sortedArrayToBST(a, left, mid-1);
root->right = sortedArrayToBST(a, mid+1, right);
return root;
}