当前位置:网站首页>LeetCode-98-验证二叉搜索树
LeetCode-98-验证二叉搜索树
2022-06-11 21:23:00 【z754916067】
题目

思路
- 一眼递归,感觉可以像之前做二叉平衡树的思路那样,判断返回的值是否为false即可,有false则一直返回false。
- 写了一版发现问题了,在于假如每两层都是符合标准的,但是三层就有可能不符合标准,如[5,4,6,null,null,3,7]这个用例。
- 题解设计的递归函数为
helper(root, lower, upper)
表示考虑以 root 为根的子树,判断子树中所有节点的值是否都在 (l,r)(l,r) 的范围内
代码
public boolean isValidBST(TreeNode root) {
boolean flag=true;
if(root.left!=null) flag = isValidDfs(root.left,Long.MIN_VALUE,root.val);
if(root.right!=null && flag==true) flag = isValidDfs(root.right,root.val,Long.MAX_VALUE);
return flag;
}
//判断以root为根的子树所有节点,是否都在(lower,upper)范围内
public boolean isValidDfs(TreeNode root,long lower,long upper){
//如果不符合要求 直接返回 不用往下找了
if(root.val<=lower || root.val>=upper) return false;
//符合要求 则看其左右子树是否符合要求
boolean flag=true;
if(root.left!=null) flag = isValidDfs(root.left,lower,root.val);
if(root.right!=null && flag==true) flag = isValidDfs(root.right,root.val,upper);
return flag;
}
边栏推荐
- Deriving Kalman filter from probability theory
- 使用 float 创建一个网页页眉、页脚、左边的内容和主要内容。
- Pyqt5 technical part - set the default value of qcombobox drop-down box and get the current selection of the drop-down box
- New product release: lr-link Lianrui launched the first 25g OCP 3.0 network card
- 【博弈论-完全信息静态博弈】 战略式博弈
- Serval and Rooted Tree(CF1153D)-DP
- 二分图King
- Codeforces Round #739 (Div. 3)解题报告
- [Part 14] source code analysis and application details of completionservice class [key]
- 正则校验匹配[0-100]、[0-1000]之间的正整数或小数点位数限制
猜你喜欢

JVM对象分配策略TLAB

Jenkins+allure integrated report construction

How to Load Data from CSV (Data Preparation Part)

Cuckoo Hash

Redis fourth session - redis high performance principle (multiplexing) and high availability analysis (backup, master-slave)

Weekly 02 | to tell you the truth, I am actually a student of MIT

How to Load Data from CSV (Data Preparation Part)

第一部分 物理层

第二部分 数据链路层

Frequency domain filter
随机推荐
Obsidian关系图谱如何让节点可以手动拖动
Weekly 02 | to tell you the truth, I am actually a student of MIT
Three waves of changes in cloud computing
【博弈论-完全信息静态博弈】 战略式博弈
One article to show you how to understand the harmonyos application on the shelves
使用 float 创建一个网页页眉、页脚、左边的内容和主要内容。
Work assessment of spectral analysis of Jilin University in March of the 22nd spring -00079
Pyqt5 technical part - cause of the problem that setting the top of the window does not take effect. Setwindowflags() does not take effect after setting the parameters. Solution
JUnit tests multithreaded code, and the sub thread does not run
Technical exchange | why should network security equipment use bypass function
可综合RTL代码设计方法和注意事项
Network security Kali penetration learning introduction to web penetration using MSF penetration to attack win7 host and execute commands remotely
一个Golang的私有库设置问题
解决 img 5px 间距的问题
LabVIEW controls Arduino to realize ultrasonic ranging (advanced chapter-5)
How to Load Data from CSV (Data Preparation Part)
二分图King
The gateway starts other microservices first. When the gateway is started, the gateway cannot be started and there is no exception log; Start the gateway first, and all services can be started normall
俩月没发过博客了,发一篇证明自己的账号还活着
select _ Lazy loading