当前位置:网站首页>leetcode刷题:二叉树21(验证二叉搜索树)
leetcode刷题:二叉树21(验证二叉搜索树)
2022-07-07 10:30:00 【涛涛英语学不进去】
98.验证二叉搜索树
给定一个二叉树,判断其是否是一个有效的二叉搜索树。
假设一个二叉搜索树具有如下特征:
- 节点的左子树只包含小于当前节点的数。
- 节点的右子树只包含大于当前节点的数。
- 所有左子树和右子树自身必须也是二叉搜索树。

把二叉树中序遍历,再把结果集遍历,如果结果集为升序,则是二叉搜索树,因为二叉搜索树的性质为 中序遍历是非递减的。本题提示 要求递增,所以不会出现相等的情况。
package com.programmercarl.tree;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
/** * @ClassName IsValidBST * @Descriotion TODO * @Author nitaotao * @Date 2022/7/5 21:32 * @Version 1.0 * https://leetcode.cn/problems/validate-binary-search-tree/ * 98. 验证二叉搜索树 **/
public class IsValidBST {
/** * 直接中序遍历获取结果 * 如果结果遍历后是 递增,则验证成功 * * @param root * @return */
public boolean isValidBST(TreeNode root) {
if (root.left == null && root.right == null) {
return true;
}
Deque<Integer> deque = new ArrayDeque<Integer>();
inorderTraversal(root, deque);
Integer pre = deque.poll();
Integer last = null;
//前一个值和后一个值比较,如果不是升序,则不是二叉搜索树
while (!deque.isEmpty()) {
last = deque.poll();
if (pre >= last) {
return false;
} else {
pre = last;
}
}
return true;
}
public void inorderTraversal(TreeNode root, Deque<Integer> deque) {
if (root == null) {
return;
}
//中序遍历 左 中 右
inorderTraversal(root.left, deque);
deque.offer(root.val);
inorderTraversal(root.right, deque);
}
}

边栏推荐
- Static comprehensive experiment
- @Bean与@Component用在同一个类上,会怎么样?
- Cenos openssh upgrade to version 8.4
- Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
- Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
- Several methods of checking JS to judge empty objects
- idm服务器响应显示您没有权限下载解决教程
- Hi3516全系统类型烧录教程
- 什么是局域网域名?如何解析?
- [play RT thread] RT thread Studio - key control motor forward and reverse rotation, buzzer
猜你喜欢

@What happens if bean and @component are used on the same class?

Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed

Upgrade from a tool to a solution, and the new site with praise points to new value

千人规模互联网公司研发效能成功之路

Introduction and application of smoothstep in unity: optimization of dissolution effect

Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier

【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移

Idea 2021 Chinese garbled code

BGP actual network configuration

Configure an encrypted web server
随机推荐
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
<No. 8> 1816. 截断句子 (简单)
PowerShell cs-utf-16le code goes online
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
Solutions to cross domain problems
Completion report of communication software development and Application
Niuke website
@Bean与@Component用在同一个类上,会怎么样?
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
Will the filing free server affect the ranking and weight of the website?
Experiment with a web server that configures its own content
开发一个小程序商城需要多少钱?
问题:先后键入字符串和字符,结果发生冲突
数据库系统原理与应用教程(007)—— 数据库相关概念
idm服务器响应显示您没有权限下载解决教程
ENSP MPLS layer 3 dedicated line
【PyTorch实战】用RNN写诗
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型