#include <iostream>
#include <vector>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
};
TreeNode* ArrayToBST(vector<int>&num,int left,int right)
{
if(left>right)
return NULL;
int mid=left+((right-left)>>1);
TreeNode* root=(TreeNode*)malloc(sizeof(TreeNode));
root->val=num[mid];
root->left=ArrayToBST(num, left, mid-1);
root->right=ArrayToBST(num, mid+1, right);
return root;
}
TreeNode *sortedArrayToBST(vector<int> &num)
{
int left=0;
int right=num.size()-1;
if(num.size()<=0)
return NULL;
TreeNode *root=ArrayToBST(num, left, right);
return root;
}
LeetCode-Convert Sorted Array to Binary Search Tree
最新推荐文章于 2021-11-02 12:48:41 发布