当前位置:网站首页>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);
}
}
边栏推荐
- 解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
- Object. Simple implementation of assign()
- 30. Few-shot Named Entity Recognition with Self-describing Networks 阅读笔记
- Attack and defense world - PWN learning notes
- NGUI-UILabel
- zero-shot, one-shot和few-shot
- The road to success in R & D efficiency of 1000 person Internet companies
- Attack and defense world ----- summary of web knowledge points
- 密码学系列之:在线证书状态协议OCSP详解
- 顶级域名有哪些?是如何分类的?
猜你喜欢
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
Simple network configuration for equipment management
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
Vxlan static centralized gateway
[statistical learning methods] learning notes - improvement methods
What is an esp/msr partition and how to create an esp/msr partition
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
随机推荐
GCC compilation error
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
Decrypt gd32 MCU product family, how to choose the development board?
什么是ESP/MSR 分区,如何建立ESP/MSR 分区
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
Tutorial on the principle and application of database system (008) -- exercises on database related concepts
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
Will the filing free server affect the ranking and weight of the website?
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
Static comprehensive experiment
TypeScript 接口继承
PowerShell cs-utf-16le code goes online
Up meta - Web3.0 world innovative meta universe financial agreement
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景