108. Convert Sorted Array to Binary Search Tree

Posted 阿怪123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了108. Convert Sorted Array to Binary Search Tree相关的知识,希望对你有一定的参考价值。

public class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        
        int len=nums.length;

        TreeNode res=buildNode(0,len-1,nums);
        
        return res;
    }
    public TreeNode buildNode(int l,int r,int[] nums)
    {
        //使用递归的方式,终止条件是 左游标大于右游标
        if(l>r)
        {
            return null;
        }
        
        
        // 将左右值相等从终止结束条件中独立出来
        int mid=(l+r)/2;
        TreeNode temp=new TreeNode(nums[mid]);
        TreeNode left=buildNode(l,mid-1,nums);
        TreeNode right=buildNode(mid+1,r,nums);
        temp.left=left;
        temp.right=right;
        return temp;
    }
}

 

以上是关于108. Convert Sorted Array to Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章

108. Convert Sorted Array to Binary Search Tree

108. Convert Sorted Array to Binary Search Tree

108. Convert Sorted Array to Binary Search Tree

leetcode 108 Convert Sorted Array to Binary Search Tree

108. Convert Sorted Array to Binary Search Tree

LC.108.Convert Sorted Array to Binary Search Tree