/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode *sortedArrayToBST(vector<int> &num) { // Start typing your C/C++ solution below // DO NOT write int main() function if(num.size()==0) return NULL; return helper(num,0,num.size()-1); } TreeNode* helper(vector<int>& num,int start,int end){ if(start>end) return NULL; int middle=start+(end-start)/2; TreeNode* node=new TreeNode(num[middle]); node->left=helper(num,start,middle-1); node->right=helper(num,middle+1,end); return node; } };
Leetcode: Convert Sorted Array to Binary Search Tree
最新推荐文章于 2020-03-28 16:06:44 发布