代码随想录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);
    }
}
相关推荐
阿镇吃橙子10 分钟前
一些手写及业务场景处理问题汇总
前端·算法·面试
酱酱哥玩AI14 分钟前
Trae编译器:实现多目标班翠鸟优化算法(IPKO)无人机路径规划仿真(Python版),完整代码
算法
MPCTHU28 分钟前
二叉树、排序算法与结构图
数据结构·算法·排序算法
亓才孓33 分钟前
[leetcode]树的操作
算法·leetcode·职场和发展
王禄DUT44 分钟前
化学方程式配平 第33次CCF-CSP计算机软件能力认证
开发语言·c++·算法
wuqingshun3141591 小时前
蓝桥杯 XYZ
数据结构·c++·算法·职场和发展·蓝桥杯
DreamByte1 小时前
C++菜鸟教程 - 从入门到精通 第五节
开发语言·c++·算法
南玖yy1 小时前
数据结构C语言练习(两个队列实现栈)
c语言·数据结构·算法
明朝百晓生1 小时前
【强化学习】【1】【PyTorch】【强化学习简介优化框架】
算法
loser~曹1 小时前
基于快速排序解决 leetcode hot215 查找数组中第k大的数字
数据结构·算法·leetcode