/**
* 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
return recursion(num, 0, num.size() - 1);
}
private:
TreeNode *recursion(vector<int> &num, int start, int end) {
if (start > end) {
return NULL;
}
int mid = (start + end) / 2;
TreeNode *root = new TreeNode(num[mid]);
root->left = recursion(num, start, mid - 1);
root->right = recursion(num, mid + 1, end);
return root;
}
};
LeetCode Convert Sorted Array to Binary Search Tree
最新推荐文章于 2020-08-07 10:05:40 发布