当前位置:网站首页>LeetCode_98_验证二叉搜索树
LeetCode_98_验证二叉搜索树
2022-07-30 13:55:00 【Fitz1318】
题目链接
题目描述
给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
- 节点的左子树只包含 小于 当前节点的数。
- 节点的右子树只包含 大于 当前节点的数。
- 所有左子树和右子树自身必须也是二叉搜索树。
示例 1:

输入:root = [2,1,3]
输出:true
示例 2:
输入:root = [5,1,4,null,null,3,6]
输出:false
解释:根节点的值是 5 ,但是右子节点的值是 4 。
提示:
- 树中节点数目范围在
[1, 10^4]内 -2^(31) <= Node.val <= 2^(31) - 1
解题思路
二叉搜索树的中序遍历是一个数值递增的有序序列
AC代码
递归法
class Solution {
TreeNode pre = null;
public boolean isValidBST(TreeNode root) {
if(root == null){
return true;
}
boolean left = isValidBST(root.left);
if(pre != null && pre.val>= root.val){
return false;
}
pre = root;
boolean right = isValidBST(root.right);
return left && right;
}
}
迭代法
class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode pre = null;
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
TreeNode tmp = stack.pop();
if (pre != null && tmp.val <= pre.val) {
return false;
}
pre = tmp;
root = tmp.right;
}
return true;
}
}
边栏推荐
- LeetCode二叉树系列——144.二叉树的最小深度
- 无代码开发平台应用可见权限设置入门教程
- Machine learning difference in the competition and industry application
- LeetCode二叉树系列——116.填充每个节点的下一个右侧指针
- How awesome is the "12306" architecture?
- Huawei's 7-year-experienced software testing director, gives some advice to all friends who want to change careers to learn software testing
- Logic Vulnerability----Permission Vulnerability
- ARC115F Migration
- 经典测试面试题集—逻辑推理题
- 如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
猜你喜欢

No-code development platform all application settings introductory tutorial

内容产品进化三板斧:流量、技术、产品形态

重保特辑|拦截99%恶意流量,揭秘WAF攻防演练最佳实践

激光雷达点云语义分割论文阅读小结

Remember an experience of interviewing an outsourcing company, should you go?

(一)Multisim安装与入门

ECCV 2022 | 通往数据高效的Transformer目标检测器

Six-faced ant financial clothing, resisting the bombardment of the interviewer, came to interview for review

Teach you how to write an eye-catching software testing resume, if you don't receive an interview invitation, I will lose

Flask框架——Flask-Mail邮件
随机推荐
关于容器的小案例
容器排序案例
Data Middle Office Construction (5): Breaking Enterprise Data Silos and Extracting Data Value
网站添加能换装可互动的live 2d看板娘
Six-faced ant financial clothing, resisting the bombardment of the interviewer, came to interview for review
自动化测试之数据驱动DDT详细篇
VLAN实验
Machine learning difference in the competition and industry application
Baijiahao cancels the function of posting documents on the interface: the weight of the plug-in chain is blocked
ECCV 2022 | 通往数据高效的Transformer目标检测器
Synology system installation related file sharing
sql server安装失败怎么办(sql server安装不了怎么办)
数据中台建设(五):打破企业数据孤岛和提取数据价值
Before quitting, make yourself a roll king
mongodb打破原则引入SQL,它到底想要干啥?
sql中ddl和dml(sql与access的区别)
AT4108 [ARC094D] Normalization
[VMware virtual machine installation mysql5.7 tutorial]
Teach you how to write an eye-catching software testing resume, if you don't receive an interview invitation, I will lose
开源工具推荐:高性能计算辅助工具MegPeak