Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
Solution:
BinaryTree* sortedArrayToBST(int arr[], int start, int end) { if (start > end) return NULL; // same as (start+end)/2, avoids overflow. int mid = start + (end - start) / 2; BinaryTree *node = new BinaryTree(arr[mid]); node->left = sortedArrayToBST(arr, start, mid-1); node->right = sortedArrayToBST(arr, mid+1, end); return node; } BinaryTree* sortedArrayToBST(int arr[], int n) { return sortedArrayToBST(arr, 0, n-1); }
LeetCode:Convert Sorted Array to Binary Search Tree,布布扣,bubuko.com
LeetCode:Convert Sorted Array to Binary Search Tree
原文:http://www.cnblogs.com/yeek/p/3619299.html