当前位置:网站首页>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;
}
边栏推荐
- Close system call analysis - Performance Optimization
- Why should garment enterprises talk about informatization?
- SPSS安装激活教程(包含网盘链接)
- 短视频系统源码,点击屏幕空白处键盘不自动收起
- Short video system source code, click the blank space of the screen, the keyboard does not automatically stow
- 【OpenGL】笔记二十九、抗锯齿(MSAA)
- sqlserver对数据进行加密、解密
- MySQL storage data encryption
- 可视化任务编排&拖拉拽 | Scaleph 基于 Apache SeaTunnel的数据集成
- Locust性能测试 —— 环境搭建及使用
猜你喜欢

Play with grpc - go deep into concepts and principles

Concurrent optimization summary

2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { fmt.Pri

Introduction and application of bigfilter global transaction anti duplication component

LOGO特训营 第三节 首字母创意手法

Concurrent network modular reading notes transfer

LOGO特训营 第五节 字体结构与设计常用技法

卷积神经网络模型之——LeNet网络结构与代码实现

UML图记忆技巧

Kdd2022 | what features are effective for interaction?
随机推荐
Easy to use app recommendation: scan QR code, scan barcode and view history
Solana链上应用Crema因黑客攻击停运
凭借了这份 pdf,最终拿到了阿里,字节,百度等八家大厂 offer
繁华落尽、物是人非:个人站长该何去何从
The use of complex numbers in number theory and geometry - Cao Zexian
Huawei Nova 10 series released Huawei application market to build a solid application security firewall
高中物理:直线运动
Recommendation of mobile app for making barcode
About stack area, heap area, global area, text constant area and program code area
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
Locust性能测试 —— 环境搭建及使用
LOGO特訓營 第一節 鑒別Logo與Logo設計思路
How to manage 15million employees easily?
PostgreSQL服务端编程聚合和分组
Test will: bug classification and promotion solution
More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
《命令行上的数据科学第二版》校对活动重新启动
Force buckle 3_ 383. Ransom letter
DevEco Device Tool 3.0 Release带来5大能力升级,让智能设备开发更高效
达梦数据凭什么被称为国产数据库“第一股”?