当前位置:网站首页>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
- SQL head injection -- injection principle and essence
- Is it safe to open an account in Ping An Securities mobile bank?
- Cenos openssh upgrade to version 8.4
- Simple network configuration for equipment management
- 数据库系统原理与应用教程(007)—— 数据库相关概念
- 平安证券手机行开户安全吗?
- The left-hand side of an assignment expression may not be an optional property access.ts(2779)
- Minimalist movie website
- PowerShell cs-utf-16le code goes online
猜你喜欢
![An error occurred when vscade tried to create a file in the target directory: access denied [resolved]](/img/14/9899f5a765872fb3238be4305a2dc7.png)
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]

跨域问题解决方案

SQL Lab (46~53) (continuous update later) order by injection
![[pytorch practice] image description -- let neural network read pictures and tell stories](/img/39/b2c61ae0668507f50426b01f2deee4.png)
[pytorch practice] image description -- let neural network read pictures and tell stories

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

File upload vulnerability - upload labs (1~2)

Preorder, inorder and postorder traversal of binary tree

百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文

Decrypt gd32 MCU product family, how to choose the development board?

Tutorial on principles and applications of database system (009) -- conceptual model and data model
随机推荐
How to understand the clothing industry chain and supply chain
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
Upgrade from a tool to a solution, and the new site with praise points to new value
跨域问题解决方案
Decrypt gd32 MCU product family, how to choose the development board?
Experiment with a web server that configures its own content
Introduction to three methods of anti red domain name generation
平安证券手机行开户安全吗?
Niuke website
Idea 2021 Chinese garbled code
<No. 8> 1816. Truncate sentences (simple)
ENSP MPLS layer 3 dedicated line
【深度学习】图像多标签分类任务,百度PaddleClas
Several methods of checking JS to judge empty objects
Simple network configuration for equipment management
对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
EPP+DIS学习之路(2)——Blink!闪烁!
In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
静态Vxlan 配置
Static vxlan configuration