当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Éducation à la transmission du savoir | Comment passer à un test logiciel pour l'un des postes les mieux rémunérés sur Internet? (joindre la Feuille de route pour l'apprentissage des tests logiciels)
Logo special training camp section III initial creative techniques
广电五舟与华为签署合作协议,共同推进昇腾AI产业持续发展
Convolutional neural network model -- lenet network structure and code implementation
Huawei Nova 10 series released Huawei application market to build a solid application security firewall
Close system call analysis - Performance Optimization
虚拟人产业面临的挑战
并发优化总结
嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
Scala download and configuration
随机推荐
Close system call analysis - Performance Optimization
HBuilder X 常用的快捷键
Domestic database chaos
UML图记忆技巧
[acwing] solution of the 58th weekly match
Shell 脚本实现应用服务日志入库 Mysql
Use blocconsumer to build responsive components and monitor status at the same time
好用app推荐:扫描二维码、扫描条形码并查看历史
Logo special training camp section III initial creative techniques
leetcode 72. Edit Distance 编辑距离(中等)
Logo special training camp section 1 Identification logo and logo design ideas
制作条形码的手机App推荐
283. Moving zero-c and language assisted array method
ApacheCN 翻译、校对、笔记整理活动进度公告 2022.7
Scala下载和配置
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this
sqlserver对数据进行加密、解密
leetcode 72. Edit Distance 编辑距离(中等)
SPSS安装激活教程(包含网盘链接)