Convert Sorted Array to Balanced Binary Search Tree (BST)

(http://leetcode.com/2010/11/convert-sorted-array-into-balanced.html)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

Code:

BinaryTree* sortedArrayToBST(int arr[], int start, int end)
{
if (start > end)
return NULL;
int mid = start + (end - start) / ;
BinaryTree* node = new BinaryTree(arr[mid]);
node->left = sortedArrayToBST(arr, start, mid-);
node->right = sortedArrayToBST(arr, mid+, end);
return node;
} BinaryTree* sortedArrayToBST(int arr[], int n)
{
return sortedArrayToBST(arr, , n-);
}
上一篇:自定义的Spring Boot starter如何设置自动配置注解


下一篇:UITableView系列(1)---Apple缓存池机制