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