当前位置:网站首页>力扣98:验证二叉搜索树
力扣98:验证二叉搜索树
2022-07-04 21:29:00 【瀛台夜雪】
力扣98:验证二叉搜索树
题目描述
给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
节点的左子树只包含 小于 当前节点的数。
节点的右子树只包含 大于 当前节点的数。
所有左子树和右子树自身必须也是二叉搜索树。
输入输出样例

输入:root = [2,1,3]
输出:true

输入:root = [5,1,4,null,null,3,6]
输出:false
解释:根节点的值是 5 ,但是右子节点的值是 4 。
解法1:使用递归的思想实现
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);
}
//使用递归的思想进行实现
bool isValidBST(TreeNode* root)
{
return isValid(root,LONG_MIN,LONG_MAX);
}
解法2:使用中序遍历
中序遍历二叉搜索树得到的也是一个升序的数组
//根据二叉搜索树中序遍历时升序的性质进行实现
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;
}
边栏推荐
- 挖财学院股票开户安全吗?开户只能在挖财开户嘛?
- MongoDB中的索引操作总结
- 刘锦程荣获2022年度中国电商行业创新人物奖
- What is the stock account opening process? Is it safe to use flush mobile stock trading software?
- GTEST from ignorance to proficiency (3) what are test suite and test case
- [public class preview]: basis and practice of video quality evaluation
- Flutter WebView示例
- Jerry added the process of turning off the touch module before turning it off [chapter]
- Cadeus has never stopped innovating. Decentralized edge rendering technology makes the metauniverse no longer far away
- Flink1.13 SQL basic syntax (I) DDL, DML
猜你喜欢

Exclusive interview of open source summer | new committer Xie Qijun of Apache iotdb community
![[public class preview]: basis and practice of video quality evaluation](/img/fd/42b98a08b5a0fd89c119f1d1a8fe1b.png)
[public class preview]: basis and practice of video quality evaluation

如何借助自动化工具落地DevOps

QT - plot other problems
![[optimtool.unconstrained] unconstrained optimization toolbox](/img/ef/65379499df205c068ee9bc9df797ac.png)
[optimtool.unconstrained] unconstrained optimization toolbox
![Compréhension approfondie du symbole [langue C]](/img/4b/26cf10baa29eeff08101dcbbb673a2.png)
Compréhension approfondie du symbole [langue C]

QT—双缓冲绘图

gtest从一无所知到熟练使用(3)什么是test suite和test case

TCP三次握手,四次挥手,你真的了解吗?

保证接口数据安全的10种方案
随机推荐
【公开课预告】:视频质量评价基础与实践
Delphi soap WebService server-side multiple soapdatamodules implement the same interface method, interface inheritance
并列图的画法,多排多列
CloudCompare&Open3D DBSCAN聚类(非插件式)
Monitor the shuttle return button
OMS系统实战的三两事
Interpreting the development of various intelligent organizations in maker Education
Bookmark
Golang interview finishing three resumes how to write
Daily question -leetcode1200- minimum absolute difference - array - sort
Why do you have to be familiar with industry and enterprise business when doing Bi development?
gtest从一无所知到熟练使用(3)什么是test suite和test case
MongoDB中的索引操作总结
Word文档中标题前面的黑点如何去掉
解决异步接口慢导致的数据错乱问题
Analysis of maker education technology in the Internet Era
Learning breakout 3 - about energy
TCP三次握手,四次挥手,你真的了解吗?
输入的查询SQL语句,是如何执行的?
Hash table