二叉树(4)

这篇博客探讨了四种与二叉树相关的算法问题:最大二叉树、合并二叉树、二叉搜索树中的搜索以及验证二叉搜索树。通过递归和迭代两种方法分别实现了这些操作,展示了在数据结构和算法领域的灵活思考。此外,还介绍了解决这些问题的优化技巧,如使用栈进行迭代,以及如何在二叉搜索树中找到最小绝对差。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.最大二叉树 leetcode 654

在这里插入图片描述

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return constructMax(nums,0,nums.length);
    }
    public TreeNode constructMax(int[] nums,int start,int end){
        if(end-start==0){
            return null;
        }
        if(end-start==1){
            return new TreeNode(nums[start]);
        }

        int max = nums[start];
        int index = start;
        for(int i=start+1;i<end;i++){
            if(max<nums[i]){
                max = nums[i];
                index = i;
            }
        }
        TreeNode root = new TreeNode(max);
        root.left = constructMax(nums,start,index);
        root.right = constructMax(nums,index+1,end);
        return root;
    }
}

2.合并二叉树 leetcode 617

在这里插入图片描述

//递归
class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1==null&&root2==null){
            return null;
        }else if(root1==null&&root2!=null){
            return root2;
        }else if(root1!=null&&root2==null){
            return root1;
        }
        TreeNode root = new TreeNode(root1.val+root2.val);
        root.left = mergeTrees(root1.left,root2.left);
        root.right = mergeTrees(root1.right,root2.right);
        return root;
    }
}
########################################################
//栈迭代
class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        Stack<TreeNode> stack =  new Stack<>();
        if(root1==null&&root2==null){
            return null;
        }
        if(root1!=null&&root2==null){
            return root1;
        }
        if(root1==null&&root2!=null){
            return root2;
        }
        stack.push(root2);
        stack.push(root1);
        while(!stack.isEmpty()){
            TreeNode cur1 = stack.pop();
            TreeNode cur2 = stack.pop();
            cur1.val = cur1.val+cur2.val;
            if(cur1.left!=null&&cur2.left!=null){
                stack.push(cur2.left);
                stack.push(cur1.left);
            }
            if(cur1.right!=null&&cur2.right!=null){
                stack.push(cur2.right);
                stack.push(cur1.right);
            }
            if(cur1.left==null&&cur2.left!=null){
                cur1.left = cur2.left;
            }
            if(cur1.right==null&&cur2.right!=null){
                cur1.right = cur2.right;
            }
        }
        return root1;
    }
}

3.二叉搜索树中的搜索 leetcode 700

在这里插入图片描述

//递归
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if(root==null){
            return null;
        }
        if(root.val==val){
            return root;
        }else if(root.val>val){
            return searchBST(root.left,val);
        }else{
            return searchBST(root.right,val);
        }
    }
}
###########################
//迭代
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if(root==null){
            return null;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode cur = stack.pop();

            if(cur.val==val){
                return cur;
            }else if(cur.val>val){
                if(cur.left!=null){
                    stack.push(cur.left);
                }
            }else{
                if(cur.right!=null){
                    stack.push(cur.right);  
                }
            }
        }
        return null;
    }
}

4.验证二叉搜索树 leetcode 98在这里插入图片描述

#递归
class Solution {
    TreeNode max;
    public boolean isValidBST(TreeNode root) {
        if(root==null){
            return true;
        }
        boolean left = isValidBST(root.left);
        if(!left){
            return false;
        }
        if(max!=null&&root.val<=max.val){
            return false;
        }
        max = root;
        boolean right = isValidBST(root.right);
        return right;

    }
}
#######################
class Solution {
    public boolean isValidBST(TreeNode root) {
        if(root==null){
            return true;
        }
        double max = -Double.MAX_VALUE;
        Stack<TreeNode> stack = new Stack<>();
        while(root!=null||!stack.isEmpty()){
            while(root!=null){
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            if(root.val<=max){
                return false;
            }
            max = root.val;
            root = root.right;
        }
        return true;
    }
}

5.二叉搜索树的最小绝对差 leetcode 530

在这里插入图片描述

class Solution {
    TreeNode pre;
    int res = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        if(root==null){
            return 0;
        }
        traversal(root);
        return res;
    }
    public void traversal(TreeNode root){
        if(root==null){
            return;
        }
        traversal(root.left);
        if(pre!=null){
            res = Math.min(res,root.val-pre.val);
        }
        pre = root;
        traversal(root.right);
    }
}
#########################################
class Solution {
    TreeNode pre=null;
    int res = Integer.MAX_VALUE;
    public int getMinimumDifference(TreeNode root) {
        if(root==null){
            return 0;
        }
        Stack<TreeNode> stack = new Stack<>();
        while(root!=null||!stack.isEmpty()){
            while(root!=null){
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            if(pre!=null){
                res = Math.min(root.val-pre.val,res);
            }
            pre = root;
            root = root.right;
        }
        return res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值