代码随想录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);
    }
}
相关推荐
EnigmaCoder9 分钟前
单链表:数据结构中的灵活“链条”
c语言·数据结构·学习
南宫生31 分钟前
力扣每日一题【算法学习day.130】
java·学习·算法·leetcode
柠石榴32 分钟前
【练习】【类似于子集问题】力扣491. 非递减子序列/递增子序列
c++·算法·leetcode·回溯
干饭高手36 分钟前
Day9,Hot100(图论)
python·leetcode·图论
!!!52540 分钟前
Java实现斗地主-做牌以及对牌排序
java·算法
sjsjs111 小时前
【数据结构-并查集】力扣1722. 执行交换操作后的最小汉明距离
数据结构·算法·leetcode
CoderIsArt1 小时前
生成一个立方体贴图(Cube Map)
算法·sharpgl
且听风吟ayan1 小时前
leetcode day20 滑动窗口209+904
算法·leetcode·c#
m0_675988231 小时前
Leetcode350:两个数组的交集 II
算法·leetcode·数组·哈希表·python3
_Itachi__1 小时前
LeetCode 热题 100 160. 相交链表
算法·leetcode·链表