代码随想录day20

654.最大二叉树

● 力扣题目地址

● 给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:

● 二叉树的根是数组中的最大元素。

● 左子树是通过数组中最大值左边部分构造出的最大二叉树。

● 右子树是通过数组中最大值右边部分构造出的最大二叉树。

● 通过给定的数组构建最大二叉树,并且输出这个树的根节点。

思路

● 我们可以给一个新的方法进行处理

代码

java 复制代码
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return construct(nums, 0, nums.length);
    }
    private TreeNode construct(int[] nums, int start, int end) {
        if (start >= end) return null; // 没有元素,返回null
        int max = nums[start]; // 从头开始
        int index = start;
        for (int i = start; i < end; i++) {
            if (nums[i] > max) {
                max = nums[i];
                index = i; // 遇到大的就更新
            }
        }
        TreeNode root = new TreeNode(max);
        root.left = construct(nums, start, index); // 递归
        root.right = construct(nums, index + 1, end);
        return root;
    }
}

617.合并二叉树

● 力扣题目链接

● 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。

思路

● 可以递归,或者使用队列或栈迭代

代码

java 复制代码
class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null) return root2;
        if (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) {
        if (root1 == null) return root2;
        if (root2 == null) return root1;
        Deque<TreeNode> queue = new ArrayDeque();
        queue.addLast(root1);
        queue.addLast(root2);
        while (!queue.isEmpty()) {
            TreeNode node1 = queue.removeFirst();
            TreeNode node2 = queue.removeFirst();
            node1.val += node2.val;
            if (node1.left != null && node2.left != null) {
                queue.addLast(node1.left);
                queue.addLast(node2.left);
            }
            if (node1.right != null && node2.right != null) {
                queue.addLast(node1.right);
                queue.addLast(node2.right);
            }
            if (node1.left == null) node1.left = node2.left;
            if (node1.right == null) node1.right = node2.right;
        }
        return root1;
    }
}

700.二叉搜索树中的搜索

● 力扣题目地址

● 给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。

思路

● 直接递归,迭代空间复杂度为O(1)

代码

java 复制代码
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if (root == null) return null;
        if (root.val == val) return root;
        return root.val > val ? searchBST(root.left, val) : searchBST(root.right, val);
    }
}

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

98.验证二叉搜索树

● 力扣题目链接

● 给定一个二叉树,判断其是否是一个有效的二叉搜索树。

思路

● 中序遍历,看是不是递增,不是递增就不是二叉搜索树

代码

java 复制代码
class Solution {
    public boolean isValidBST(TreeNode root) {
        Deque<TreeNode> stack = new ArrayDeque();
        long num = Long.MIN_VALUE;
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.addFirst(root);
                root = root.left;
            }
            root = stack.removeFirst();
            if (root.val <= num) return false;
            num = root.val;
            root = root.right;
        }
        return true;
    }
}

class Solution {
    TreeNode max;
    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        if (!isValidBST(root.left)) return false;
        if (max != null && root.val <= max.val) return false;
        max = root;
        return isValidBST(root.right);
    }
}
相关推荐
ShiinaMashirol2 小时前
代码随想录打卡|Day27(合并区间、单调递增的数字、监控二叉树)
java·算法
wuqingshun3141594 小时前
蓝桥杯 5. 交换瓶子
数据结构·c++·算法·职场和发展·蓝桥杯
Demons_kirit4 小时前
Leetcode 2845 题解
算法·leetcode·职场和发展
adam_life5 小时前
http://noi.openjudge.cn/——2.5基本算法之搜索——200:Solitaire
算法·宽搜·布局唯一码
我想进大厂6 小时前
图论---朴素Prim(稠密图)
数据结构·c++·算法·图论
我想进大厂6 小时前
图论---Bellman-Ford算法
数据结构·c++·算法·图论
AIGC大时代6 小时前
高效使用DeepSeek对“情境+ 对象 +问题“型课题进行开题!
数据库·人工智能·算法·aigc·智能写作·deepseek
lkbhua莱克瓦246 小时前
用C语言实现——一个中缀表达式的计算器。支持用户输入和动画演示过程。
c语言·开发语言·数据结构·链表·学习方法·交友·计算器
CODE_RabbitV6 小时前
【深度强化学习 DRL 快速实践】近端策略优化 (PPO)
算法
Wendy_robot7 小时前
【滑动窗口+哈希表/数组记录】Leetcode 438. 找到字符串中所有字母异位词
c++·算法·leetcode