当前位置:网站首页>Li Kou 98: verify binary search tree
Li Kou 98: verify binary search tree
2022-07-04 22:32:00 【Yingtai night snow】
Power button 98: Verify binary search tree
Title Description
Give you the root node of a binary tree root , Determine whether it is an effective binary search tree .
It works The binary search tree is defined as follows :
The left subtree of the node contains only Less than Number of current nodes .
The right subtree of the node contains only Greater than Number of current nodes .
All left and right subtrees themselves must also be binary search trees .
I/o sample

Input :root = [2,1,3]
Output :true

Input :root = [5,1,4,null,null,3,6]
Output :false
explain : The value of the root node is 5 , But the value of the right child node is 4 .
solution 1: Using the idea of recursion
bool isValid(TreeNode *root,long long lower,long long upper)
{
if(root==nullptr)
{
return true;
}
if(root->val<=lower||root->val>=upper)
{
return false;
}
return isValid(root->left,lower,root->val)&&isValid(root->right,root->val,upper);
}
// Use the idea of recursion to realize
bool isValidBST(TreeNode* root)
{
return isValid(root,LONG_MIN,LONG_MAX);
}
solution 2: Using middle order traversal
Traversing the binary search tree in medium order, we get an ascending array
// According to the property of ascending order when traversing the binary search tree
bool isValidBST2(TreeNode* root)
{
if(!root)
{
return true;
}
stack<TreeNode *>stk;
vector<int>res;
while(!stk.empty()||root!=nullptr)
{
while(root!=nullptr)
{
stk.push(root);
root=root->left;
}
TreeNode *temp;
temp=stk.top();
// cout<<temp->val<<" ";
res.push_back(temp->val);
stk.pop();
root=temp->right;
}
int length=res.size();
for(int i=1;i<length;i++)
{
if(res[i]<=res[i-1])
{
return false;
}
}
return true;
}
边栏推荐
- Introduction and application of bigfilter global transaction anti duplication component
- 283. Moving zero-c and language assisted array method
- LOGO特训营 第四节 字体设计的重要性
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- DevEco Device Tool 3.0 Release带来5大能力升级,让智能设备开发更高效
- Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game
- [cooking record] - stir fried 1000 pieces of green pepper
- Logo special training camp section 1 Identification logo and logo design ideas
- leetcode 72. Edit Distance 编辑距离(中等)
- 凭借了这份 pdf,最终拿到了阿里,字节,百度等八家大厂 offer
猜你喜欢

可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成

Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game

More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?

Deveco device tool 3.0 release brings five capability upgrades to make intelligent device development more efficient

Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code

【OpenGL】笔记二十九、抗锯齿(MSAA)

传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)
![[advanced C language] array & pointer & array written test questions](/img/3f/83801afba153cfc0dd73aa8187ef20.png)
[advanced C language] array & pointer & array written test questions

【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用

LOGO特训营 第一节 鉴别Logo与Logo设计思路
随机推荐
Introducing QA into the software development lifecycle is the best practice that engineers should follow
Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development
PostgreSQL JOIN实践及原理
i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
TLA+ 入门教程(1):形式化方法简介
智洋创新与华为签署合作协议,共同推进昇腾AI产业持续发展
关于栈区、堆区、全局区、文字常量区、程序代码区
sqlserver对数据进行加密、解密
Detailed explanation of flask context
Solana链上应用Crema因黑客攻击停运
Force buckle_ Palindrome number
How to manage 15million employees easily?
Mysql root 账号如何重置密码
好用app推荐:扫描二维码、扫描条形码并查看历史
广电五舟与华为签署合作协议,共同推进昇腾AI产业持续发展
Common open source codeless testing tools
我在linux里面 通过调用odspcmd 查询数据库信息 怎么静默输出 就是只输出值 不要这个
Logo special training camp section III initial creative techniques
idea中pom.xml依赖无法导入