Day17--二叉树--654. 最大二叉树,617. 合并二叉树,700. 二叉搜索树中的搜索,98. 验证二叉搜索树

Day17--二叉树--654. 最大二叉树,617. 合并二叉树,700. 二叉搜索树中的搜索,98. 验证二叉搜索树

654. 最大二叉树

思路:

前序遍历。寻找子数组的区间。注意区间要统一成习惯。这里是左闭右开。

java 复制代码
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return build(nums, findIndexOfMaxValue(nums));
    }

    private TreeNode build(int[] nums, int index) {
        if (nums.length == 0) {
            return null;
        }
        if (nums.length == 1) {
            return new TreeNode(nums[0]);
        }
        TreeNode root = new TreeNode(nums[index]);
        int[] leftArray = Arrays.copyOfRange(nums, 0, index);
        int[] rightArray = Arrays.copyOfRange(nums, index + 1, nums.length);
        root.left = build(leftArray, findIndexOfMaxValue(leftArray));
        root.right = build(rightArray, findIndexOfMaxValue(rightArray));
        return root;
    }

    private int findIndexOfMaxValue(int[] nums) {
        int max = Integer.MIN_VALUE;
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] > max) {
                max = nums[i];
                index = i;
            }
        }
        return index;
    }
}

617. 合并二叉树

思路:

用栈迭代,前序遍历。

把node2的值加到node1上,如果一方是有节点一方是null,创建一个节点赋值为0.

java 复制代码
class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        // 处理null情况
        if(root1==null&&root2==null){
            return null;
        }
        if(root1==null){
            return root2;
        }
        if(root2==null){
            return root1;
        }
        Deque<TreeNode> stack = new ArrayDeque<>();
        stack.push(root2);
        stack.push(root1);
        while(!stack.isEmpty()){
            TreeNode node1 = stack.pop();
            TreeNode node2 = stack.pop();
            node1.val += node2.val;
            if(node1.left==null&&node2.left!=null){
                node1.left = new TreeNode(0);
            }else if(node1.left!=null&&node2.left==null){
                node2.left = new TreeNode(0);
            }
            if(node1.left!=null&&node2.left!=null){
                stack.push(node2.left);
                stack.push(node1.left);
            }
            if(node1.right==null&&node2.right!=null){
                node1.right = new TreeNode(0);
            }else if(node1.right!=null&&node2.right==null){
                node2.right = new TreeNode(0);
            }
            if(node1.right!=null&&node2.right!=null){
                stack.push(node2.right);
                stack.push(node1.right);
            }
        }
        return root1;
    }
}

700. 二叉搜索树中的搜索

思路:

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

98. 验证二叉搜索树

思路:

中序遍历

java 复制代码
class Solution {

    private long prev = Long.MIN_VALUE;

    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        if (!isValidBST(root.left)) {
            return false;
        }
        if (prev >= root.val) {
            return false;
        }
        prev = root.val;
        return isValidBST(root.right);
    }
}
相关推荐
倒头就睡的小比特4 天前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!4 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼4 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
m0_547486664 天前
《数据结构教程》全套 PPT课件2026
数据结构
旖旎夜光4 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark4 天前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her14 天前
并查集-听课笔记
笔记·算法·并查集
码流子4 天前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
another heaven4 天前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
wzdark4 天前
从算法设计模式看编程思维的抽象能力4
算法