当前位置:网站首页>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);
}
}
边栏推荐
- Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
- 跨域问题解决方案
- 静态Vxlan 配置
- About sqli lab less-15 using or instead of and parsing
- ps链接图层的使用方法和快捷键,ps图层链接怎么做的
- 盘点JS判断空对象的几大方法
- Using stack to convert binary to decimal
- 数据库系统原理与应用教程(009)—— 概念模型与数据模型
- gcc 编译报错
- Typescript interface inheritance
猜你喜欢
数据库系统原理与应用教程(011)—— 关系数据库
Inverted index of ES underlying principle
Sonar:cognitive complexity
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
Processing strategy of message queue message loss and repeated message sending
(待会删)yyds,付费搞来的学术资源,请低调使用!
Airserver automatically receives multi screen projection or cross device projection
RHSA first day operation
静态Vxlan 配置
Experiment with a web server that configures its own content
随机推荐
College entrance examination composition, high-frequency mention of science and Technology
利用棧來實現二進制轉化為十進制
数据库系统原理与应用教程(011)—— 关系数据库
Static vxlan configuration
Vxlan static centralized gateway
问题:先后键入字符串和字符,结果发生冲突
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
On valuation model (II): PE index II - PE band
About web content security policy directive some test cases specified through meta elements
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
Attack and defense world ----- summary of web knowledge points
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
数据库系统原理与应用教程(009)—— 概念模型与数据模型
Up meta - Web3.0 world innovative meta universe financial agreement
【深度学习】图像多标签分类任务,百度PaddleClas
跨域问题解决方案
ENSP MPLS layer 3 dedicated line
The left-hand side of an assignment expression may not be an optional property access. ts(2779)