【二叉树part06】| 654.最大二叉树、617.合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树

news/2024/5/19 21:28:00 标签: 数学建模, java, 算法, 数据结构, 推荐算法

目录

🎈LeetCode654.最大二叉树

🎈LeetCode617.合并二叉树

🎈LeetCode700. 二叉搜索树中的搜索

🎈LeetCode98. 验证二叉搜索树


🎈LeetCode654.最大二叉树

链接:654.最大二叉树

给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:

  1. 创建一个根节点,其值为 nums 中的最大值。
  2. 递归地在最大值 左边 的 子数组前缀上 构建左子树。
  3. 递归地在最大值 右边 的 子数组后缀上 构建右子树。

返回 nums 构建的 最大二叉树 

 

 还是用递归三部曲做,代码如下:

java">public TreeNode constructMaximumBinaryTree(int[] nums) {
        // 1 <= nums.length <= 1000
        return bulid(nums,0,nums.length-1);
    }
    public TreeNode bulid(int[] nums,int start,int end){
        // 0 <= nums[i] <= 1000
        if(start>end){
            return null;
        }
        if(start==end){
            return new TreeNode(nums[start]);
        }
        int max=0;
        int index=0;
        for(int i=start;i<=end;i++){
            if(nums[i]>max){
                max=nums[i];
                index=i;
            }
        }
        TreeNode root=new TreeNode(max);
        root.left=bulid(nums,start,index-1);
        root.right=bulid(nums,index+1,end);
        return root;
    }

🎈LeetCode617.合并二叉树

链接:617.合并二叉树

给你两棵二叉树: root1 和 root2 。

想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。

返回合并后的二叉树。

注意: 合并过程必须从两个树的根节点开始。

java">public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        TreeNode root=new TreeNode();
        if(root1==null && root2==null){
            return null;
        }else if(root1==null && root2!=null){
            return root2;
            // return new TreeNode(root2.val);
        }else if(root1!=null && root2==null){
            return root1;
            // return new TreeNode(root1.val);
        }else{
            root.val=root1.val+root2.val;
            // return new TreeNode(root1.val+root2.val);
            root.left= mergeTrees(root1.left,root2.left);
            root.right=mergeTrees(root1.right,root2.right);
        }
        return root;
    }

🎈LeetCode700. 二叉搜索树中的搜索

链接:700.二叉搜索树中的搜索

 给定二叉搜索树(BST)的根节点 root 和一个整数值 val

你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。

 

java">public TreeNode searchBST(TreeNode root, int val) {
        //root 是二叉搜索树 
        if(root==null){
            return null;
        }
        TreeNode node=new TreeNode(0);
        if(root.val>val){
           node=searchBST(root.left,val);
        }else if(root.val<val){
            node=searchBST(root.right,val);
        }else{
            return root;
        }
        return node;
    }

🎈LeetCode98. 验证二叉搜索树

链接:98.验证二叉搜索树

给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。

有效 二叉搜索树定义如下:

  • 节点的左子树只包含 小于 当前节点的数。
  • 节点的右子树只包含 大于 当前节点的数。
  • 所有左子树和右子树自身必须也是二叉搜索树。

 

二叉搜索树的中序遍历正好是一个递增的序列,可以利用中序遍历来判断是不是二叉搜索树,代码如下: 

java">public TreeNode searchBST(TreeNode root, int val) {
        //递归法
        //root 是二叉搜索树 
        if(root==null){
            return null;
        }
        TreeNode node=new TreeNode(0);
        if(root.val>val){
           node=searchBST(root.left,val);
        }else if(root.val<val){
            node=searchBST(root.right,val);
        }else{
            return root;
        }
        return node;
    }
java">public boolean isValidBST(TreeNode root) {
        // 迭代法
        if(root==null){
            return true;
        }
        Stack<TreeNode> st=new Stack<>();
        st.push(root);
        TreeNode pre=null;
        while(!st.isEmpty()){
            TreeNode node=st.peek();
            if(node!=null){
                st.pop();
                if(node.right!=null){
                    st.push(node.right);
                }
                st.push(node);
                st.push(null);
                if(node.left!=null){
                    st.push(node.left);
                }
            }else{
                st.pop();
                TreeNode temp=st.pop();
                if(pre!=null && pre.val>=temp.val){
                    return false;
                }
                pre=temp;
            }
        }
        return true;
    }

http://www.niftyadmin.cn/n/472325.html

相关文章

可重入,可打断,公平锁,条件变量原理解读

目录 可重入原理 可打断原理 不可打断模式 可打断模式 公平锁实现原理 条件变量实现原理 await 流程 signal 流程 可重入原理 什么是可重入&#xff1a;当线程请求一个由其它线程持有的对象锁时&#xff0c;该线程会阻塞&#xff0c;而当线程请求由自己持有的对象锁…

Spring Boot 中的 @ComponentScan 注解是什么,原理,如何使用

Spring Boot 中的 ComponentScan 注解是什么&#xff0c;原理&#xff0c;如何使用 在 Spring Boot 中&#xff0c;ComponentScan 是一种注解&#xff0c;它可以让 Spring 自动扫描指定的包及其子包中的组件&#xff0c;并将这些组件自动装配到 Spring 容器中。本文将介绍 Com…

flutter获取键盘按键和触摸操作的事件

flutter获取键盘按键和触摸操作的事件. 借助于Listener和RawKeyboardListener import package:flutter/cupertino.dart; import package:flutter/gestures.dart; import package:flutter/material.dart; import package:flutter/services.dart;void main() {runApp(const MyA…

2023网安面试题164道(附答案)

最近有不少小伙伴跑来咨询&#xff1a; 想找网络安全工作&#xff0c;应该要怎么进行技术面试准备&#xff1f;工作不到 2 年&#xff0c;想跳槽看下机会&#xff0c;有没有相关的面试题呢&#xff1f; 为了更好地帮助大家高薪就业&#xff0c;今天就给大家分享两份网络安全工…

stm32最常用网站

ST官网&#xff1a;www.st.com ST社区&#xff1a;www.stmcu.org.cn ST中文网&#xff1a;www.stmcu.com.cn

【Java 基础篇】Java Set 详解

文章目录 导言一、Set 概述二、HashSet三、TreeSet四、LinkedHashSet总结 导言 在Java的集合框架中&#xff0c;Set接口是一个无序、不可重复的集合&#xff0c;它扩展了Collection接口&#xff0c;并提供了一系列操作和方法来处理元素的集合。本文将详细介绍Java中的Set接口及…

Vue Router 相关理解 基本路由 多级路由

6.1.相关理解 6.1.1.vue-router 的理解 vue的一个插件库&#xff0c;专门用来实现SPA应用 6.1.2.对SPA应用的理解 单页Web应用&#xff08;single page web application&#xff0c;SPA&#xff09;整个应用只有一个完整的页面点击页面中的导航链接不会刷新页面&#xff0c…

请求头配置记录

1、如果需要将前端头像上传的请求头配置与其他请求头配置合并到一起 可以使用以下方法&#xff1a; 定义一个公共的请求头配置对象&#xff0c;包含所有需要应用到所有请求中的头部信息&#xff0c;例如&#xff1a; const commonHeaders {Authorization: Bearer xxxx,Acce…