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

Posted stAr_1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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;
    }

以上是关于[leetcode]108. Convert Sorted Array to Binary Search Tree构建二叉搜索树的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 108. Convert Sorted Array to Binary Search Tree

Leetcode 108. Convert Sorted Array to Binary Search Tree

LeetCode_108. Convert Sorted Array to Binary Search Tree

[LeetCode]题解(python):108-Convert Sorted Array to Binary Search Tree

Leetcode 108. Convert Sorted Array to Binary Search Tree

leetcode108. Convert Sorted Array to Binary Search Tree