力扣刷题记录 -- JAVA--63---108. 将有序数组转换为二叉搜索树


一、题目

在这里插入图片描述

在这里插入图片描述

二、代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution 
{
    //获得了中序遍历数组  每次取中间构造就可以了
    public void build_tree(TreeNode pro, int[] before,int[] after)
    {
        int left_length = before.length;
        int right_length = after.length;

        if(left_length!=0)
        {
           

            TreeNode left = new TreeNode(before[left_length/2]);

            System.out.println("left 节点值  "+left.val);

            pro.left = left;
            int before_length = left_length/2;
            int after_length = left_length -1 - left_length/2;

            if(before_length<0) before_length = 0;
            if(after_length<0) after_length = 0;

            int[] before_left = new int[before_length];
            int[] after_left = new int[after_length];
            int i;
            for(i=0;i<before_length;i++)
            {
                before_left[i] = before[i];
            }
            for(i=left_length/2+1;i<left_length;i++)
            {
                after_left[i-left_length/2-1] = before[i];
            }

             build_tree(pro.left, before_left, after_left);
        }
        if(right_length!= 0)
        {
           

            TreeNode right = new TreeNode(after[right_length/2]);

            System.out.println("right 节点值  "+right.val);

            pro.right =right;

            int before_length = right_length/2;
            int after_length = right_length -1 - right_length/2;

            if(before_length<0) before_length = 0;
            if(after_length<0) after_length = 0;

            int[] before_right = new int[before_length];
            int[] after_right = new int[after_length];

            int i;
            for(i=0;i<before_length;i++)
            {
                before_right[i] = after[i];
            }
            for(i=right_length/2+1;i<right_length;i++)
            {
                after_right[i-right_length/2-1] = after[i];
            }

             build_tree(pro.right, before_right, after_right);
        }
        
        // return pro;
    }
    public TreeNode sortedArrayToBST(int[] nums) 
    {
        if(nums.length==0) return null;
        int length = nums.length;
        TreeNode root = new TreeNode(nums[length/2]);
         
        System.out.println("root 节点值  "+root.val);
        int before_length = length/2;
        int after_length = length -1 - length/2;

        if(before_length<0) before_length = 0;
        if(after_length<0) after_length = 0;

        int[] before = new int[before_length];
        int[] after = new int[after_length];

        int i;
        for(i=0;i<before_length;i++)
        {
            before[i] = nums[i];
        }
        for(i=length/2+1;i<length;i++)
        {
            after[i-length/2-1] = nums[i];
        }


        build_tree(root, before, after);

        return root;

    }
}

三、运行结果

在这里插入图片描述

四、附录

二刷

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution 
{
    public TreeNode build(int[] temp)
    {
        if(temp.length == 0 ) return null;
        int i;
        int length = temp.length;

        int mid = length/2;
        int[] left = new int[mid];
        int[] right = new int[length-1-mid];
        
        TreeNode root = new TreeNode(temp[mid]);
        for(i=0;i<mid;i++) left[i] = temp[i];
        for(i=mid+1;i<length;i++) right[i-mid-1] = temp[i];
        root.left = build(left);
        root.right = build(right);
        
        return root;
    }
    public TreeNode sortedArrayToBST(int[] nums) 
    {
        int i;
        int length;
        Arrays.sort(nums);
        length = nums.length;

        //for(i=0;i<length;i++) System.out.println("  "+nums[i]); 
        
        return build(nums);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@白圭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值