代码随想录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);
    }
}
相关推荐
学java的小菜鸟啊11 分钟前
第五章 网络编程 TCP/UDP/Socket
java·开发语言·网络·数据结构·网络协议·tcp/ip·udp
菜鸟求带飞_42 分钟前
算法打卡:第十一章 图论part01
java·数据结构·算法
浅念同学43 分钟前
算法.图论-建图/拓扑排序及其拓展
算法·图论
是小Y啦1 小时前
leetcode 106.从中序与后续遍历序列构造二叉树
数据结构·算法·leetcode
程序猿练习生1 小时前
C++速通LeetCode中等第9题-合并区间
开发语言·c++·leetcode
liuyang-neu1 小时前
力扣 42.接雨水
java·算法·leetcode
y_dd1 小时前
【machine learning-12-多元线性回归】
算法·机器学习·线性回归
m0_631270401 小时前
标准c语言(一)
c语言·开发语言·算法
万河归海4281 小时前
C语言——二分法搜索数组中特定元素并返回下标
c语言·开发语言·数据结构·经验分享·笔记·算法·visualstudio
小周的C语言学习笔记1 小时前
鹏哥C语言36-37---循环/分支语句练习(折半查找算法)
c语言·算法·visual studio