当前位置:网站首页>Leetcode problem solving -- 98 Validate binary search tree
Leetcode problem solving -- 98 Validate binary search tree
2022-07-06 03:07:00 【Snowy solitary boat】
Recursive version
public boolean isValidBST(TreeNode root) {
return range(root,Integer.MIN_VALUE-1L,Integer.MAX_VALUE+1L);
}
public boolean range(TreeNode node,long min,long max){
if (node==null) return true;
if (node.val<=min||node.val>=max) return false;
return range(node.left,min,node.val)&&range(node.right,node.val,max);
}
Wrong thinking :
Before saying the right way of thinking , Let me first talk about the pit I stepped on , I was thinking about recursion , Then judge whether the value of the left node is greater than or equal to the root node or the value of the right node is less than or equal to the root node , Just go back to false, But in fact, this is not possible , Please look at the chart below.
7 / \ 2 5 / \ 1 9
According to the above idea, the result is true, This is because the upper limit of the left subtree is ignored , And the right subtree has the error caused by the lower limit
The right way of thinking :
Use the upper and lower limits to judge
- Because the root node has no upper and lower limits , Therefore, the maximum and minimum values that can be stored by the variable type are used as the upper and lower limits
- If the node is null Go straight back to true
- If the node is not null, And the node value is not within the range specified by the upper and lower limits ,return false
- Continue to judge subtree , But should pay attention to , The upper limit of the left subtree is the value of the current subtree , The lower limit of the right subtree is the value of the current subtree
Iteration version
public boolean isValidBST(TreeNode root) {
Stack<TreeNode> stack = new Stack<>();
TreeNode node = root;
long prev = Integer.MIN_VALUE-1L;
while (!stack.isEmpty()||node!=null){
while (node!=null){
stack.push(node);
node = node.left;
}
node = stack.pop();
if (node.val<=prev) return false;
prev = node.val;
node = node.right;
}
return true;
}
Ideas :
BSTree In the middle order traversal of , The value of the previous node to be traversed must be less than the value of the next node to be traversed , Therefore, a node that stores the value of the previous traversed node is added prev Variables for comparison
This scheme is based on the understanding of the iterative method of middle order traversal , If a little partner doesn't know , And I want to know more about the explanation idea of middle order traversal iteration , Sure Click here to jump
边栏推荐
- 主数据管理(MDM)的成熟度
- 【若依(ruoyi)】设置主题样式
- Classic interview question [gem pirate]
- Codeworks 5 questions per day (1700 average) - day 6
- 【Kubernetes 系列】一文學會Kubernetes Service安全的暴露應用
- [kubernetes series] learn the exposed application of kubernetes service security
- 【若依(ruoyi)】ztree 自定义图标(iconSkin 属性)
- CSP numeric sort
- 2.11 simulation summary
- IPv6 jobs
猜你喜欢
![[network security interview question] - how to penetrate the test file directory through](/img/48/be645442c8ff4cc5417c115963b217.jpg)
[network security interview question] - how to penetrate the test file directory through

Qt发布exe软件及修改exe应用程序图标

QT release exe software and modify exe application icon
![Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]](/img/3b/385d19e51340ecd6281df47b39f40c.png)
Huawei, H3C, Cisco command comparison, mind map form from the basic, switching, routing three directions [transferred from wechat official account network technology alliance station]

Analyze menu analysis

深入探究指针及指针类型

CobaltStrike-4.4-K8修改版安装使用教程

2022工作中遇到的问题四

1. Dynamic parameters of function: *args, **kwargs
![[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University](/img/0f/520242492524522c887b6576463566.jpg)
[Chongqing Guangdong education] higher mathematics I reference materials of Southwest Petroleum University
随机推荐
CSP numeric sort
Sign SSL certificate as Ca
MySQL advanced notes
[ruoyi] ztree custom icon (iconskin attribute)
【若依(ruoyi)】启用迷你导航栏
2.13 simulation summary
2.12 simulation
Apt installation ZABBIX
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
2.11 simulation summary
Universal crud interface
【Unity3D】GUI控件
Self made CA certificate and SSL certificate using OpenSSL
What is the investment value of iFLYTEK, which does not make money?
[ruoyi] set theme style
js 正则过滤和增加富文本中图片前缀
微服务注册与发现
tcpdump: no suitable device found
Communication between microservices
Function knowledge points