LeetCode:654.最大二叉树 && 617.合并二叉树 && 700.二叉搜索树中的搜索 && 98.验证二叉搜索树

654.最大二叉树

题目

给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:
创建一个根节点,其值为 nums 中的最大值。
递归地在最大值 左边 的 子数组前缀上 构建左子树。
递归地在最大值 右边 的 子数组后缀上 构建右子树。
返回 nums 构建的 最大二叉树 。
在这里插入图片描述

递归

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return traversal(nums, 0, nums.length);
    }
    public TreeNode traversal(int[] nums, int leftIndex, int rightIndex){
        //小于1,表示已经没有元素了
        if(rightIndex - leftIndex < 1) return null;
        //等于1,只有一个元素
        if(rightIndex - leftIndex == 1) return new TreeNode(nums[leftIndex]);
        //找出最大值的位置
        int maxIndex = leftIndex;
        //找出最大值
        int maxVal = nums[maxIndex];
        for(int i = leftIndex + 1; i < rightIndex; i++){
            if(nums[i] > maxVal){
                maxVal = nums[i];
                maxIndex = i;
            }
        }
        //创建节点,将最大值当做根节点
        TreeNode node = new TreeNode(maxVal);
        //用maxIndex作为分界线,划分左右子树
        node.left = traversal(nums, leftIndex, maxIndex);
        node.right = traversal(nums, maxIndex + 1, rightIndex);
        return node;
    }
}

617.合并二叉树

题目

给你两棵二叉树: root1 和 root2 。

想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。
返回合并后的二叉树。
注意: 合并过程必须从两个树的根节点开始
在这里插入图片描述

递归(前序遍历,中、后遍历也可以)

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 mergeTrees(TreeNode root1, TreeNode root2) {
        if(root1 == null) return root2;
        if(root2 == null) return root1;
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        stack1.push(root1);
        stack2.push(root2);
        while(!stack1.isEmpty()){
            TreeNode node1 = stack1.pop();
            TreeNode node2 = stack2.pop();
            node1.val += node2.val;。
            。
            if(node1.left != null && node2.left != null){
                stack1.push(node1.left);
                stack2.push(node2.left);
            }else{
                if(node1.left == null){
                    node1.left = node2.left;
                }
            }
            if(node1.right != null && node2.right != null){
                stack1.push(node1.right);
                stack2.push(node2.right);
            }else{
                if(node1.right == null){
                    node1.right = node2.right;
                }
            }
        }
        return root1;
    }
}

700.二叉搜索树中的搜索

题目

给定二叉搜索树(BST)的根节点 root 和一个整数值 val。
你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。
在这里插入图片描述

递归

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

迭代

class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        while(root != null){
            if(root.val > val) {
                root = root.left;
            }else if(root.val < val) {
                root = root.right;
            }else {
                return root;
            }
        }
        return null;
    }
}

98.验证二叉搜索树

题目

给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
节点的左子树只包含 小于 当前节点的数。
节点的右子树只包含 大于 当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
在这里插入图片描述

递归

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;
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        while(root != null || !stack.isEmpty()){
            while(root != null){
                stack.push(root);
                //左
                root = root.left;
            }
            //中
            TreeNode node = stack.pop();
            if(pre != null && node.val <= pre.val) return false;
            pre = node;
            //右
            root = node.right;
        }
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值