Convert Sorted Array to Binary Search Tree || LeetCode

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* built_bst(int* nums,int start,int end){
    int mid;
    mid=(start+end)/2;
    if(start>end)return NULL;
    struct TreeNode *p;
    p=(struct TreeNode*)malloc(sizeof(struct TreeNode));
    p->val=nums[mid];
    p->left=built_bst(nums,start,mid-1);
    p->right=built_bst(nums,mid+1,end );
    return p;
}
struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
    struct TreeNode *root;
    root=built_bst(nums,0,numsSize-1);
    return root;
}

  

posted on 2015-06-08 16:40  coderlt  阅读(114)  评论(0编辑  收藏  举报

导航