1. 程式人生 > >建立一顆最小二叉查詢樹

建立一顆最小二叉查詢樹

struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x):val(x),left(NULL),right(NULL){}
};


TreeNode *createBST(vector<int> arr,int start,int end)
{


if(start>end)
return NULL;
int mid=(start+end)/2;
TreeNode *node=new TreeNode(arr[mid]);
node->left=createBST(arr,start,mid-1);
node->right=createBST(arr,mid+1,end);
return node;
}


TreeNode *create(vector<int> arr)
{
int n=arr.size();
return createBST(arr,0,n-1);
}