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

1. LeetCode 654.最大二叉树

题目链接:https://leetcode.cn/problems/maximum-binary-tree/
文章链接:https://programmercarl.com/0654.最大二叉树.html
视频链接:https://www.bilibili.com/video/BV1MG411G7ox

在这里插入图片描述

思路:使用前序遍历递归构造二叉树。
每次递归找出当前数组的位置范围中的最大值,作为当前节点对象,然后按照最大值位置切割数组,进行下一次递归,获取当前对象的左右节点对象。
注意:
1️⃣构造树一般采用的是前序遍历,因为先构造中间节点,然后递归构造左子树和右子树。
2️⃣每次分隔不用定义新的数组,而是通过下标索引直接在原数组上操作。

解法:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return find(nums,0,nums.length);
    }

    public TreeNode find(int[] nums,int start,int end) {
        if (start == end) {
            return null;
        }

        int max = -1;
        int index = 0;
        for (int i = start;i < end;i++) {
            if (nums[i] > max) {
                max = nums[i];
                index = i;
            }
        }
        // 中
        TreeNode cur = new TreeNode(max);

        // 切割数组
        int leftStart = start;
        int leftEnd = index;
        int rightStart = index + 1;
        int rightEnd = end;

        // 左
        cur.left = find(nums,leftStart,leftEnd);
        // 右
        cur.right = find(nums,rightStart,rightEnd);

        return cur;
    }
}

2. LeetCode 617.合并二叉树

题目链接:https://leetcode.cn/problems/merge-two-binary-trees/description/
文章链接:https://programmercarl.com/0617.合并二叉树.html
视频链接:https://www.bilibili.com/video/BV1m14y1Y7JK

在这里插入图片描述

思路:使用前序遍历递归。
针对传入的两个节点,根据他们是否为空,进行相应的操作,返回节点。

解法:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null && root2 == null) {
            return null;
        }
        if (root1 == null) {
            return root2;
        } else if (root2 == null) {
            return root1;
        }

        // root1和root2都不为空
        // 中
        TreeNode cur = new TreeNode(root1.val+root2.val); // 当前节点

        // 左
        cur.left = mergeTrees(root1.left,root2.left);
        // 右
        cur.right = mergeTrees(root1.right,root2.right);

        return cur;
    }
}

3. LeetCode 700.二叉搜索树中的搜索

题目链接:https://leetcode.cn/problems/search-in-a-binary-search-tree/description/
文章链接:https://programmercarl.com/0700.二叉搜索树中的搜索.html
视频链接:https://www.bilibili.com/video/BV1wG411g7sF

在这里插入图片描述

思路:递归方式,并利用二叉搜索树的性质。
二叉搜索树是一个有序树:
1️⃣若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
2️⃣若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
3️⃣它的左、右子树也分别为二叉搜索树。

解法:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    // public TreeNode searchBST(TreeNode root, int val) {
    //     if (root == null) return null;

    //     // 中
    //     if (root.val == val) return root;

    //     // 左
    //     TreeNode leftRes = searchBST(root.left, val);
    //     if (leftRes != null) {
    //         return leftRes;
    //     }

    //     // 右
    //     TreeNode rightRes = searchBST(root.right, val);
    //     if (rightRes != null) {
    //         return rightRes;
    //     } 

    //     return null;
    // }

    // 根据二叉搜索树性质
    public TreeNode searchBST(TreeNode root, int val) {
        if (root == null) return null;

        // 中
        if (root.val == val) return root;

        if (root.val < val) {
            return searchBST(root.right, val);
        } else {
            return searchBST(root.left, val);
        }
    }
}

4. LeetCode 98.验证二叉搜索树

题目链接:https://leetcode.cn/problems/validate-binary-search-tree/description/
文章链接:https://programmercarl.com/0098.验证二叉搜索树.html
视频链接:https://www.bilibili.com/video/BV18P411n7Q4

在这里插入图片描述

思路:
解题关键是二叉搜索树使用中序遍历是单调递增的,不存在重复数字。
每次递归都只需要判断当前节点的数字是否大于最大值,若大于,则说明当前节点及之前的所有节点满足二叉搜索树;当最后一个节点大于最大值时,则代表整个树满足二叉搜索树。

解法:
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {

    TreeNode pre = null;

    // 中序遍历
    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;

        // 左
        boolean left = isValidBST(root.left);

        // 中        
        if (pre != null && pre.val >= root.val) {
            return false;
        }

        pre = root;

        // 右
        boolean right = isValidBST(root.right);

        return left && right;
    }
}
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值