题目描述:
//用一个升序的数组 构建 高度平衡的儿茶搜索树
//用递归和二分的思想
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* sortedArrayToBST(vector<int>& nums) { return dfs(nums,0,nums.size()-1); } TreeNode *dfs(vector<int> &nums, int start, int end){ if(start > end) return NULL; int mid = start + (end - start)/2; TreeNode *node = new TreeNode(nums[mid]); node->left = dfs(nums,start,mid-1); node->right = dfs(nums,mid+1,end); return node; } };