1、题目描述
2、问题分析
使用二分法即可。
3、代码
1 TreeNode* sortedArrayToBST(vector<int>& nums) { 2 if (nums.size() == 0) 3 return NULL; 4 5 int left = 0; 6 int right = nums.size() - 1; 7 8 TreeNode *node = new TreeNode(0); 9 BST(node->left,nums, left, right); 10 return node->left; 11 12 13 14 } 15 16 void BST(TreeNode * &parent, vector<int> &nums,int left, int right) 17 { 18 19 if (left <= right) { 20 int mid = (left + right)/2; 21 TreeNode *node = new TreeNode(nums[mid]); 22 parent = node; 23 BST(node->left, nums, left, mid - 1); 24 BST(node->right, nums, mid + 1, right); 25 } else { 26 return ; 27 } 28 }