代码随想录算法训练营day20 | 654.最大二叉树,617.合并二叉树, 700.二叉搜索树中的搜索, 98.验证二叉搜索树

654. 最大二叉树
package _06binary_tree.day20._01construct_maximum_binarytree;

import _06binary_tree.TreeNode;

public class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return traverse(nums, 0, nums.length);
    }

    private TreeNode traverse(int[] nums, int left, int right) {
        // 递归终止条件
        if (right - left < 1) return null;
        if (right - left == 1) return new TreeNode(nums[0]);

        // 求分割点
        int maxIndex = left;
        int maxValue = nums[maxIndex];
        for (int i = left + 1; i < right; i++) {
            if (nums[i] > maxValue) {
                maxIndex = i;
                maxValue = nums[i];
            }
        }

        TreeNode root = new TreeNode(maxValue);
        root.left = traverse(nums, left, maxIndex);
        root.right = traverse(nums, maxIndex + 1, right);

        return root;
    }
}
617. 合并二叉树
package _06binary_tree.day20._02merge_tree;

import _06binary_tree.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

public class Solution {
    // 递归
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null) return root2;
        if (root2 == null) return root1;

        root1.val += root2.val; // 中
        root1.left = mergeTrees(root1.left, root2.left); // 左
        root1.right = mergeTrees(root1.right, root2.right); // 右
        return root1;
    }

    // 迭代(层序)
    public TreeNode mergeTrees2(TreeNode root1, TreeNode root2) {
        if (root1 == null) return root2;
        if (root2 == null) return root1;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root1);
        queue.offer(root2);
        while (!queue.isEmpty()){
            TreeNode node1 = queue.poll();
            TreeNode node2 = queue.poll();
            node1.val += node2.val;

            if(node1.left != null && node2.left != null){
                queue.offer(node1.left);
                queue.offer(node2.left);
            }
            if(node1.right != null && node2.right != null){
                queue.offer(node1.right);
                queue.offer(node2.right);
            }
            if(node1.left == null){
                node1.left = node2.left;
            }
            if(node1.right == null){
                node1.right = node2.right;
            }
        }
        return root1;
    }
}
700. 二叉搜索树中的搜索
package _06binary_tree.day20._03search_bst;

import _06binary_tree.TreeNode;

public class Solution {
    // 递归
    public TreeNode searchBST(TreeNode root, int val) {
        if (root == null || root.val == val) return root;
        if (val < root.val) return searchBST(root.left, val);
        if (val > root.val) return searchBST(root.right, val);
        // 找不到返回null
        return null;
    }

    // 迭代
    public TreeNode searchBST2(TreeNode root, int val) {
        while (root != null) {
            if(val < root.val){
                root = root.left;
            }else if(val > root.val){
                root = root.right;
            }else {
                return root;
            }
        }
        return null;
    }
}
98. 验证二叉搜索树
package _06binary_tree.day20._04is_valid_bst;

import _06binary_tree.TreeNode;

public class Solution {
    long prev = Long.MIN_VALUE;

    // 递归
    public boolean isValidBST(TreeNode root) {
        if(root == null)return true;
        if(!isValidBST(root.left))return false; // 左
        if(root.val <= prev)return false; // 中
        prev = root.val;
        return isValidBST(root.right); // 右
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值