当前位置:网站首页>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;
}
边栏推荐
- Detailed explanation of flask context
- idea中pom.xml依赖无法导入
- 如何实现轻松管理1500万员工?
- odps 中 对表进行了一次备份,为什么在元数据库查m_table 时,两张表的逻辑大小不一致,但数
- 达梦数据凭什么被称为国产数据库“第一股”?
- leetcode 72. Edit Distance 编辑距离(中等)
- Nat. Commun.| Machine learning jointly optimizes the affinity and specificity of mutagenic therapeutic antibodies
- # 2156. Find the substring of the given hash value - post order traversal
- Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
- 【Acwing】第58场周赛 题解
猜你喜欢
AscendEX 上线 Walken (WLKN) - 一款卓越领先的“Walk-to-Earn”游戏
【OpenGL】笔记二十九、抗锯齿(MSAA)
卷积神经网络模型之——LeNet网络结构与代码实现
More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development
Google Earth Engine(GEE)——Tasks升级,实现RUN ALL可以一键下载任务类型中的所有影像
【Acwing】第58场周赛 题解
抖音实战~评论数量同步更新
Use blocconsumer to build responsive components and monitor status at the same time
达梦数据凭什么被称为国产数据库“第一股”?
随机推荐
AscendEX 上线 Walken (WLKN) - 一款卓越领先的“Walk-to-Earn”游戏
leetcode 72. Edit Distance 编辑距离(中等)
Interview question 01.08 Zero matrix
嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
Force buckle 3_ 383. Ransom letter
我在linux里面 通过调用odspcmd 查询数据库信息 怎么静默输出 就是只输出值 不要这个
制作条形码的手机App推荐
【烹饪记录】--- 青椒炒千张
php短视频源码,点赞时会有大拇指动画飘起
The table is backed up in ODPs. Why check m in the metabase_ Table, the logical sizes of the two tables are inconsistent, but the number of
抖音实战~评论数量同步更新
Challenges faced by virtual human industry
机器人相关课程考核材料归档实施细则2022版本
High school physics: linear motion
醒悟的日子,我是怎么一步一步走向软件测试的道路
Machine learning notes mutual information
Concurrent network modular reading notes transfer
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
Tiktok actual combat ~ the number of comments is updated synchronously
PHP short video source code, thumb animation will float when you like it