代码苏随想录训练营第二十天|654.最大二叉树 ● 617.合并二叉树 ● 700.二叉搜索树中的搜索 ● 98.验证二叉搜索树

最大二叉树

import java.util.Arrays;

public class day20_654_最大二叉树 {

    public static 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;
        }
    }
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return digui(nums);



    }
    TreeNode digui(int[] num){
        //先把最大值找出来
        TreeNode demores;
        if (num.length==0) {
            return null;
            
        }
        //接下来找最大值
        int index=0;
        int max=Integer.MIN_VALUE;
        for(int i=0;i<num.length;i++){
            if (num[i]>max) {
                max=num[i];
                index=i;
                
            }
        }
        //经过这里找到了最大值
        //接下来就是构造二叉树了
        demores=new TreeNode(max);//这里创建了加载最大数字的节点
        if (num.length==1) {
            return demores;
            //这里是如果长度就是1,直接就返回节点
        }
        int[] left=Arrays.copyOfRange(num, 0, index);
        int[] right=Arrays.copyOfRange(num, index+1, num.length);
        //这里把左右找出来了
        demores.left=digui(left);
        demores.right=digui(right);
        return demores;






    }

    
}

这题的思路很简单,理解了构造二叉树就行,就是单纯的递归,最最大值取出放在节点里面,然后吧最大值的左右递归进去,一直这样下去就行了

合并二叉树

public class day20_617_合并二叉树 {
    public static 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;
        }
    }
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        

        return digui(root1,root2);

    }
    TreeNode digui(TreeNode root1,TreeNode root2){
        TreeNode demores=new TreeNode();
        if (root1==null&&root2==null) {
            return null;

            
        }//首先判断是不是都是空的。是的话就返回null
        if (root1!=null&&root2==null) {
            demores.val=root1.val;
            demores.left=digui(root1.left, null);
            demores.right=digui(root1.right, null);
            
            
        }else if (root1==null&&root2!=null) {
            demores.val=root2.val;
            demores.left=digui(null, root2.left);
            demores.right=digui(null, root2.right);
            
        }else{
            demores.val=root1.val+root2.val;
            demores.left=digui(root1.left, root2.left);
            demores.right=digui(root1.right, root2.right);
        }
        return demores;





    }


    
}

这题也是寻常的递归,但是有一个不同点就是分情况递归,根据root的不同情况也是常规的递归步骤  处理 左子树 右子树这样的顺序,只不过根据了当前root的情况去写 了

class Solution {
    // 递归
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null) return root2;
        if (root2 == null) return root1;

        root1.val += root2.val;
        root1.left = mergeTrees(root1.left,root2.left);
        root1.right = mergeTrees(root1.right,root2.right);
        return root1;
    }
}

这个写法同理,不过是更简便你的

二叉搜索树中的搜做

class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if (root == null) {
            return null;
        }
        if (val == root.val) {
            return root;
        }
        return searchBST(val < root.val ? root.left : root.right, val);
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/search-in-a-binary-search-tree/solutions/1121178/er-cha-sou-suo-shu-zhong-de-sou-suo-by-l-d8zi/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
/**
 * 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 {
    TreeNode res;
    public TreeNode searchBST(TreeNode root, int val) {
        digui(root,val);
        return res;

    }
    void digui(TreeNode root,int val){
        
        if (root==null) {
            return;
            
        }
        if (root.val==val) {
            res=root;
            return;
            
        }
        digui(root.left, val);
        digui(root.right, val);




    }
}

这是官方的自己的方法,官方的递归要仔细思考一下

验证二擦搜索树

public class day20_98_验证二叉搜索树 {
    public static 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;
        }
    }
    long min=Long.MIN_VALUE;
    boolean res=true;
    public boolean isValidBST(TreeNode root) {
        digui(root);
        return res;


    }
    void digui(TreeNode root1){
        //中序遍历看看是不是二叉搜索树
        if (root1==null) {
            return;
            
        }
        digui(root1.left);
        if (root1.val<=min) {
            res=false;
            
        }
        min=root1.val;
        digui(root1.right);
        

    }

    
}

卡尔思路是中序遍历就是一个严格的递增的,所以只要不是递增,就不行

去递归的中序遍历,然后判断每次的是不是符合要求

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值