当前位置:网站首页>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;
}
边栏推荐
- The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
- 智洋创新与华为签署合作协议,共同推进昇腾AI产业持续发展
- Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
- 国产数据库乱象
- leetcode 72. Edit distance edit distance (medium)
- SPSS安装激活教程(包含网盘链接)
- 2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri
- 面试必备 LeetCode 链表算法题汇总,全程干货!
- How to manage 15million employees easily?
- 力扣_回文数
猜你喜欢

How to transfer to software testing, one of the high paying jobs in the Internet? (software testing learning roadmap attached)

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

Logo special training camp section 1 Identification logo and logo design ideas

Logo special training camp Section IV importance of font design

【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用

复数在数论、几何中的用途 - 曹则贤

Visual task scheduling & drag and drop | scalph data integration based on Apache seatunnel

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

With this PDF, we finally got offers from eight major manufacturers, including Alibaba, bytek and Baidu
随机推荐
虚拟人产业面临的挑战
不同环境相同配置项的内容如何diff差异?
广电五舟与华为签署合作协议,共同推进昇腾AI产业持续发展
Sqlserver encrypts and decrypts data
The proofreading activity of data science on the command line second edition was restarted
Locust performance test - environment construction and use
LOGO特训营 第五节 字体结构与设计常用技法
1807. Replace the parentheses in the string
Logo special training camp section III initial creative techniques
2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri
Microservices -- Opening
Tiktok actual combat ~ the number of comments is updated synchronously
《命令行上的数据科学第二版》校对活动重新启动
傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
将QA引入软件开发生命周期是工程师要遵循的最佳实践
ACM multimedia 2022 | counterfactual measurement and elimination of social prejudice in visual language pre training model
Locust性能测试 —— 环境搭建及使用
Easy to use app recommendation: scan QR code, scan barcode and view history
Close system call analysis - Performance Optimization
Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game