[leetcode]108. Convert Sorted Array to Binary Search Tree构建二叉搜索树

构建二叉搜索树

/*
       利用二叉搜索树的特点:根节点是中间的数
       每次找到中间数,左右子树递归子数组
     */
    public TreeNode sortedArrayToBST(int[] nums) {
        return builder(nums,0,nums.length-1);
    }
    public TreeNode builder(int[] nums,int left,int right)
    {
        if (left>right)
            return null;
        int mid = (left+right)/2;
        TreeNode temp = new TreeNode(nums[mid]);
        temp.left = builder(nums,left,mid-1);
        temp.right = builder(nums,mid+1,right);
        return temp;
    }
posted @ 2018-01-24 09:48  stAr_1  阅读(143)  评论(0编辑  收藏  举报