代码随想录|654.最大二叉树 617.合并二叉树 700.二叉搜索树中的搜索 98.验证二叉搜索树

递归三部曲:
  1. 确定递归函数的参数和返回值
  2. 确定终止条件
  3. 确定单层递归的逻辑(二叉树中要确定前中后序遍历)
public class Day15 {
    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    //给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:
    //创建一个根节点,其值为 nums 中的最大值。
    //递归地在最大值 左边 的 子数组前缀上 构建左子树。
    //递归地在最大值 右边 的 子数组后缀上 构建右子树。
    //返回 nums 构建的 最大二叉树 。
    public TreeNode constructMaximumBinaryTree(int[] nums) {

        return constructMaxBinTra(nums,0,nums.length-1);
    }
    public TreeNode constructMaxBinTra(int[] nums,int begin,int end){
        if(begin>end)
            return null;
        int max=nums[begin];
        int index=begin;
        for(int i=begin;i<=end;i++){
            if(max<nums[i]) {
                max = nums[i];
                index=i;
            }
        }
        TreeNode root=new TreeNode(nums[index]);
        root.left=constructMaxBinTra(nums,begin,index-1);
        root.right=constructMaxBinTra(nums,index+1,end);
        return root;
    }
    //给你两棵二叉树: root1 和 root2 。
    //想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。
    // 你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,
    // 那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。
    //返回合并后的二叉树。
    //注意: 合并过程必须从两个树的根节点开始。
    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;
    }
    //给定二叉搜索树(BST)的根节点 root 和一个整数值 val。
    //你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。
    public TreeNode searchBST(TreeNode root, int val) {
        if(root==null)
            return null;
        if(root.val>val)
            return searchBST(root.left,val);
        else if(root.val<val)
            return searchBST(root.right,val);
        else return root;//二叉搜索树
//        if(root==null||root.val==val)
//            return root;
//        TreeNode left=searchBST(root.left,val);
//        if(left!=null)
//            return left;
//        return searchBST(root.right,val);普通二叉树
    }
    //给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
    //有效 二叉搜索树定义如下:
    //节点的左
    //子树
    //只包含 小于 当前节点的数。
    //节点的右子树只包含 大于 当前节点的数。
    //所有左子树和右子树自身必须也是二叉搜索树。
    TreeNode pre=null;
    public boolean isValidBST(TreeNode root) {//二叉搜索树中序遍历是一个递增的有序数组
        if(root==null)
            return true;
//        if(root.left!=null&&root.left.val>=root.val||root.right!=null&&root.right.val<=root.val)
//            return false;//代码错误:二叉搜索树右子树全部都要大于左子树,不能只判断根与子树关系
        boolean left=isValidBST(root.left);//左

        if(pre!=null&&pre.val>=root.val)//既不能大于也不能等于
            return false;
        pre=root;//中

        boolean right=isValidBST(root.right);//右
        return left&&right;
    }
}
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值