当前位置:网站首页>力扣练习——58 验证二叉搜索树
力扣练习——58 验证二叉搜索树
2022-08-10 11:00: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。
每个结点的数据不超过32位int型数的表示范围。
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;//考虑到部分样例数值会超过INT
while (!stack.empty() || root != NULL) {
while (root != NULL) {
stack.push(root);
root = root->left;//遍历到最左下元素
}
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;
}
边栏推荐
- 企业如何判断数据治理是否成功?
- Get started quickly and conquer three different distributed architecture calling schemes
- rider内Mono脚本找不到引用资源
- 一文带你搞懂中断按键驱动程序之poll机制
- Break through the dimensional barriers and let the dolls around you move on the screen!
- 使用JMeter进行MySQL的压力测试
- L2 applications from a product perspective: why is it a playground?
- 第5章相似矩阵及二次型(4)
- LeetCode_628_三个数的最大乘积
- ENVI 5.3软件安装包和安装教程
猜你喜欢

Short video software development - how to break the platform homogenization

EasyCVR级联时,修改下级平台名称将不同步至上级平台

STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建

mysql5.7安装部署-yum安装

8月份DB-Engines 数据库排行榜最新战况

VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决

【勇敢饭饭,不怕刷题之链表】有序链表的合并

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

Several small projects that I have open sourced over the years

中小规模网站架构
随机推荐
力扣练习——62 有效的数独
Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
从产品维度来看 我们为什么不能完全信任Layer2?
JWT implements login authentication + Token automatic renewal scheme
【勇敢饭饭,不怕刷题之链表】有序链表的合并
如何使用工程仪器设备在线监测管理系统
2023版揽胜运动曝光,安全、舒适一个不落
POJ 1026 Cipher (置换群)
3款不同类型的自媒体免费工具,有效提高创作、运营效率
关于振弦采集模块及采集仪振弦频率值准确率的问题
什么是幂等性?四种接口幂等性方案详解!
1-IMU参数解析以及选择
学长告诉我,大厂MySQL都是通过SSH连接的
短视频软件开发——平台同质化如何破局
2022年裁员潮,失业程序员何去何从?
Cybersecurity Notes 5 - Digital Signatures
一文带你搞懂中断按键驱动程序之poll机制
JWT 实现登录认证 + Token 自动续期方案
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
第2章-矩阵及其运算-矩阵创建(1)