当前位置:网站首页>力扣练习——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;
}
边栏推荐
猜你喜欢
负载均衡原理分析与源码解读
动作捕捉系统用于室内组合定位技术研究
如何使用工程仪器设备在线监测管理系统
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan
Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
自媒体爆款标题怎么写?手把手教你写热门标题
LAXCUS分布式操作系统安全管理
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细
随机推荐
老板加薪!看我做的WPF Loading!!!
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
【TypeScript】接口类型与类型别名:这两者的用法与区别分别是什么?
金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案
推荐6个自媒体领域,轻松易上手
POJ 1026 Cipher (置换群)
负载均衡原理分析与源码解读
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create
快速上手,征服三种不同分布式架构调用方案
企业如何判断数据治理是否成功?
Will SQL and NoSQL eventually converge?
Open Office XML 格式里如何描述多段具有不同字体设置的段落
Cybersecurity Notes 5 - Digital Signatures
不止跑路,拯救误操作rm -rf /*的小伙儿
Double.doubleToLongBits() method uses
GPU accelerated Pinterest recommendation model, the number of parameters increased by 100 times, and the user activity increased by 16%
Double.doubleToLongBits()方法使用
HDU 1520 Anniversary party (树型dp)
从源码角度分析UUID的实现原理