码迷,mamicode.com
首页 > 其他好文 > 详细

Convert Sorted Array to Binary Search Tree || LeetCode

时间:2015-06-08 19:06:09      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * 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;
}

  

Convert Sorted Array to Binary Search Tree || LeetCode

标签:

原文地址:http://www.cnblogs.com/ProtectedDream/p/4561338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!