当前位置:网站首页>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;
}
边栏推荐
- Easy to use app recommendation: scan QR code, scan barcode and view history
- 制作条形码的手机App推荐
- Why is Dameng data called the "first share" of domestic databases?
- TLA+ 入门教程(1):形式化方法简介
- md5工具类
- MySQL存储数据加密
- Nat. Commun.| Machine learning jointly optimizes the affinity and specificity of mutagenic therapeutic antibodies
- 并发网络模块化 读书笔记转
- DevEco Device Tool 3.0 Release带来5大能力升级,让智能设备开发更高效
- 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
猜你喜欢

Logo special training camp section II collocation relationship between words and graphics

Radio and television Wuzhou signed a cooperation agreement with Huawei to jointly promote the sustainable development of shengteng AI industry

国产数据库乱象

【Acwing】第58场周赛 题解

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

传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)

É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

LOGO特訓營 第一節 鑒別Logo與Logo設計思路
![[Yugong series] go teaching course 003-ide installation and basic use in July 2022](/img/65/b36ca239e06a67c01d1eb0b4648f71.png)
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
随机推荐
繁华落尽、物是人非:个人站长该何去何从
Huawei Nova 10 series released Huawei application market to build a solid application security firewall
将QA引入软件开发生命周期是工程师要遵循的最佳实践
i. Mx6ull driver development | 24 - platform based driver model lights LED
PostgreSQLSQL高级技巧透视表
达梦数据凭什么被称为国产数据库“第一股”?
繁華落盡、物是人非:個人站長該何去何從
LOGO特训营 第三节 首字母创意手法
LOGO特训营 第一节 鉴别Logo与Logo设计思路
现在mysql cdc2.1版本在解析值为0000-00-00 00:00:00的datetime类
Introducing QA into the software development lifecycle is the best practice that engineers should follow
力扣3_383. 赎金信
PostgreSQL JOIN实践及原理
[cooking record] - stir fried 1000 pieces of green pepper
leetcode 72. Edit distance edit distance (medium)
嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
Solana链上应用Crema因黑客攻击停运
Easy to use app recommendation: scan QR code, scan barcode and view history
É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)
醒悟的日子,我是怎么一步一步走向软件测试的道路