当前位置:网站首页>Licking Exercise - 58 Verifying Binary Search Trees
Licking Exercise - 58 Verifying Binary Search Trees
2022-08-10 11:33:00 【qq_43403657】
58 验证二叉搜索树
1.问题描述
给定一个二叉树,判断其是否是一个有效的二叉搜索树.
一个二叉搜索树具有如下特征:
节点的左子树只包含小于当前节点的数.
节点的右子树只包含大于当前节点的数.
所有左子树和右子树自身必须也是二叉搜索树.
示例 1:
输入:
2
/ \
1 3
输出: true
示例 2:
输入:
5
/ \
1 4
/ \
3 6
输出: false
解释: 输入为: [5,1,4,null,null,3,6].
根节点的值为 5 ,但是其右子节点值为 4 .
可使用以下main函数:
#include
#include
#include
#include
#include
#include
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(NULL), right(NULL) {}
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
TreeNode* inputTree()
{
int n,count=0;
char item[100];
cin>>n;
if (n==0)
return NULL;
cin>>item;
TreeNode* root = new TreeNode(atoi(item));
count++;
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (count<n)
{
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
cin>>item;
count++;
if (strcmp(item,"null")!=0)
{
int leftNumber = atoi(item);
node->left = new TreeNode(leftNumber);
nodeQueue.push(node->left);
}
if (count==n)
break;
cin>>item;
count++;
if (strcmp(item,"null")!=0)
{
int rightNumber = atoi(item);
node->right = new TreeNode(rightNumber);
nodeQueue.push(node->right);
}
}
return root;
}
int main()
{
TreeNode* root;
root=inputTree();
bool res=Solution().isValidBST(root);
cout<<(res?"true":"false")<<endl;
}
2.输入说明
首先输入结点的数目n(注意,这里的结点包括题中的null空结点)
然后输入n个结点的数据,需要填充为空的结点,输入null.
The data of each node does not exceed32位intRepresentation range of type numbers.
3.输出说明
输出true或false
4.范例
输入
7
5 1 4 null null 3 6
输出
false
5.代码
#include <iostream>
#include <queue>
#include <climits>
#include<cstdlib>
#include <cstring>
#include<map>
#include<stack>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(NULL), right(NULL) {
}
TreeNode(int x) : val(x), left(NULL), right(NULL) {
}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {
}
};
TreeNode* inputTree()
{
int n, count = 0;
char item[100];
cin >> n;
if (n == 0)
return NULL;
cin >> item;
TreeNode* root = new TreeNode(atoi(item));
count++;
queue<TreeNode*> nodeQueue;
nodeQueue.push(root);
while (count < n)
{
TreeNode* node = nodeQueue.front();
nodeQueue.pop();
cin >> item;
count++;
if (strcmp(item, "null") != 0)
{
int leftNumber = atoi(item);
node->left = new TreeNode(leftNumber);
nodeQueue.push(node->left);
}
if (count == n)
break;
cin >> item;
count++;
if (strcmp(item, "null") != 0)
{
int rightNumber = atoi(item);
node->right = new TreeNode(rightNumber);
nodeQueue.push(node->right);
}
}
return root;
}
bool isValidBST(TreeNode *root)
{
stack<TreeNode*> stack;//用栈实现中序遍历
long long pre = (long long)INT_MIN - 1;//Considering that some sample values will exceedINT
while (!stack.empty() || root != NULL) {
while (root != NULL) {
stack.push(root);
root = root->left;//Traverse to the bottom left element
}
root = stack.top();
stack.pop();
// 如果中序遍历得到的节点的值小于等于前一个 pre,说明不是二叉搜索树
if (root->val <= pre) {
return false;
}
pre = root->val;
root = root->right;
}
return true;
}
int main()
{
TreeNode* root;
root = inputTree();
bool res = isValidBST(root);
cout << (res ? "true" : "false") << endl;
}
边栏推荐
- ENVI 5.3软件安装包和安装教程
- OSSCore 开源解决方案介绍
- Where can I view the version record of WeChat applet submission review history?
- Several small projects that I have open sourced over the years
- 推荐6个自媒体领域,轻松易上手
- 【机器学习】浅谈正规方程法&梯度下降
- YTU 2894: G--我要去内蒙古大草原
- 快速上手,征服三种不同分布式架构调用方案
- runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
- 学长告诉我,大厂MySQL都是通过SSH连接的
猜你喜欢

关于振弦采集模块及采集仪振弦频率值准确率的问题

蔚来-软件开发工程师一面记录

快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?

AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)

Mobile and PC compatible loading and toast message plugins
![[Go WebSocket] 多房间的聊天室(一)思考篇](/img/c9/4374a57c6a4ae02f606253a4c299e4.png)
[Go WebSocket] 多房间的聊天室(一)思考篇

Nocalhost - 让云原生时代的开发更高效

Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability

runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function

基于UiAutomator2+PageObject模式开展APP自动化测试实战
随机推荐
基于UiAutomator2+PageObject模式开展APP自动化测试实战
什么是幂等性?四种接口幂等性方案详解!
Pycharm终端出现PS问题、conda或activate不是内部命令问题..
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
Research on motion capture system for indoor combined positioning technology
第5章相似矩阵及二次型(4)
内存问题难定位,那是因为你没用ASAN
WeChat applet, global variables change in one place and the state in other places also changes.
3款不同类型的自媒体免费工具,有效提高创作、运营效率
用proteus直接仿真stm32-可以完全丢弃编程器
CPU多级缓存与缓存一致性
Mobile and PC compatible loading and toast message plugins
The brave rice rice, does not fear the brush list of 】 list has a ring
从源码角度分析UUID的实现原理
英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
蔚来-软件开发工程师一面记录
力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using
POJ 2891 Strange Way to Express Integers (Extended Euclidean)
力扣练习——62 有效的数独