当前位置:网站首页>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);
}
}
边栏推荐
- "Series after reading" my God! It's so simple to understand throttling and anti shake~
- Completion report of communication software development and Application
- 问题:先后键入字符串和字符,结果发生冲突
- wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
- Apache installation problem: configure: error: APR not found Please read the documentation
- Using stack to convert binary to decimal
- Learning and using vscode
- idea 2021中文乱码
- 全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
- @Bean与@Component用在同一个类上,会怎么样?
猜你喜欢
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
[statistical learning methods] learning notes - improvement methods
【PyTorch实战】图像描述——让神经网络看图讲故事
"Series after reading" my God! It's so simple to understand throttling and anti shake~
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios
Configure an encrypted web server
Introduction and application of smoothstep in unity: optimization of dissolution effect
Tutorial on principles and applications of database system (007) -- related concepts of database
随机推荐
ps链接图层的使用方法和快捷键,ps图层链接怎么做的
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
@Bean与@Component用在同一个类上,会怎么样?
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
Up meta - Web3.0 world innovative meta universe financial agreement
平安证券手机行开户安全吗?
30. Feed shot named entity recognition with self describing networks reading notes
Niuke website
Hi3516全系统类型烧录教程
Typescript interface inheritance
Hi3516 full system type burning tutorial
数据库系统原理与应用教程(009)—— 概念模型与数据模型
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
How to understand the clothing industry chain and supply chain
全球首堆“玲龙一号”反应堆厂房钢制安全壳上部筒体吊装成功
Epp+dis learning road (2) -- blink! twinkle!
Utiliser la pile pour convertir le binaire en décimal
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
(to be deleted later) yyds, paid academic resources, please keep a low profile!
Learning and using vscode