当前位置:网站首页>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;
}
边栏推荐
- 卷积神经网络模型之——LeNet网络结构与代码实现
- 好用app推荐:扫描二维码、扫描条形码并查看历史
- 现在mysql cdc2.1版本在解析值为0000-00-00 00:00:00的datetime类
- How can the advertising system of large factories be upgraded without the presence of large models
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- 高中物理:直线运动
- 传智教育|如何转行互联网高薪岗位之一的软件测试?(附软件测试学习路线图)
- B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条
- leetcode 72. Edit Distance 编辑距离(中等)
- LOGO特训营 第一节 鉴别Logo与Logo设计思路
猜你喜欢
Xiangjiang Kunpeng joined the shengteng Wanli partnership program and continued to write a new chapter of cooperation with Huawei
It is said that software testing is very simple, but why are there so many dissuasions?
Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel
虚拟人产业面临的挑战
Kdd2022 | what features are effective for interaction?
A large number of virtual anchors in station B were collectively forced to refund: revenue evaporated, but they still owe station B; Jobs was posthumously awarded the U.S. presidential medal of freedo
With this PDF, we finally got offers from eight major manufacturers, including Alibaba, bytek and Baidu
湘江鲲鹏加入昇腾万里伙伴计划,与华为续写合作新篇章
Challenges faced by virtual human industry
Enabling digital economy Fuxin software attends the BRICs high level Forum on Sustainable Development
随机推荐
LOGO特训营 第三节 首字母创意手法
É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)
MD5 tool class
The use of complex numbers in number theory and geometry - Cao Zexian
Concurrent optimization summary
常用的开源无代码测试工具
How can the advertising system of large factories be upgraded without the presence of large models
机器人相关课程考核材料归档实施细则2022版本
How to manage 15million employees easily?
Apachecn translation, proofreading, note sorting activity progress announcement 2022.7
With this PDF, we finally got offers from eight major manufacturers, including Alibaba, bytek and Baidu
Solana chain application crema was shut down due to hacker attacks
i. Mx6ull driver development | 24 - platform based driver model lights LED
POM in idea XML dependency cannot be imported
LOGO特訓營 第一節 鑒別Logo與Logo設計思路
不同环境相同配置项的内容如何diff差异?
Jvm-Sandbox-Repeater的部署
Microservices -- Opening
Logo Camp d'entraînement section 3 techniques créatives initiales
LOGO特训营 第五节 字体结构与设计常用技法