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);
    }
}
相关推荐
地平线开发者5 小时前
J6B vio scenario sample
算法
BothSavage17 小时前
Trae远程开发中DeepSeek自定义模型4054错误的排查与修复
算法
小林ixn17 小时前
从暴力到KMP:一道题彻底搞懂字符串匹配的前世今生
算法
烬羽19 小时前
字符串算法入门:从反转字符串到回文判断,面试不再慌
算法·面试
先吃饱再说1 天前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰2 天前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术2 天前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六2 天前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程