代码随想录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);
    }
}
相关推荐
int型码农4 小时前
数据结构第八章(一) 插入排序
c语言·数据结构·算法·排序算法·希尔排序
UFIT5 小时前
NoSQL之redis哨兵
java·前端·算法
喜欢吃燃面5 小时前
C++刷题:日期模拟(1)
c++·学习·算法
SHERlocked935 小时前
CPP 从 0 到 1 完成一个支持 future/promise 的 Windows 异步串口通信库
c++·算法·promise
怀旧,5 小时前
【数据结构】6. 时间与空间复杂度
java·数据结构·算法
积极向上的向日葵5 小时前
有效的括号题解
数据结构·算法·
GIS小天5 小时前
AI+预测3D新模型百十个定位预测+胆码预测+去和尾2025年6月7日第101弹
人工智能·算法·机器学习·彩票
_Itachi__5 小时前
LeetCode 热题 100 74. 搜索二维矩阵
算法·leetcode·矩阵
不忘不弃5 小时前
计算矩阵A和B的乘积
线性代数·算法·矩阵
不爱写代码的玉子5 小时前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#