标签:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
[Solution]
1 TreeNode *sortedArrayToBST(vector<int> &num) 2 { 3 return BSTtranslate(num, 0, num.size() - 1); 4 } 5 6 TreeNode *BSTtranslate(vector<int> &num, int left, int right) 7 { 8 TreeNode *root = NULL; 9 if (left > right) 10 return NULL; 11 12 int middle = (left + right) / 2; 13 root = new TreeNode(num[middle]); 14 root->left = BSTtranslate(num, left, middle - 1); 15 root->right = BSTtranslate(num, middle + 1, right); 16 return root; 17 }
leetcode 108. Convert Sorted Array to Binary Search Tree
标签:
原文地址:http://www.cnblogs.com/ym65536/p/4292209.html