当前位置:网站首页>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;
}
边栏推荐
- Alibaba最新神作!耗时182天肝出来1015页分布式全栈手册太香了
- WeChat applet, global variables change in one place and the state in other places also changes.
- 暑期总结4
- What is affecting MySQL performance?
- 负载均衡原理分析与源码解读
- OneFlow source code parsing: operator instructions executed in a virtual machine
- MLX90640 红外热成像仪测温传感器 手机 APP 软件 RedEye 连接详细
- POJ 1026 Cipher (Permutation Groups)
- POJ 3101 Astronomy (Mathematics)
- 不止跑路,拯救误操作rm -rf /*的小伙儿
猜你喜欢

做自媒体月入几万?博主们都在用的几个自媒体工具

C#实战:基于ItextSharp技术标签生成小工具

L2 applications from a product perspective: why is it a playground?

【勇敢饭饭,不怕刷题之链表】链表倒数节点问题

使用哈工大LTP测试分词并且增加自定义字典

第5章相似矩阵及二次型(4)

谷歌数据中心发生“电力事故”造成 3 人受伤

接口定义与实现

基于UiAutomator2+PageObject模式开展APP自动化测试实战

From the product dimension, why can't we fully trust Layer2?
随机推荐
第5章相似矩阵及二次型(4)
GPU accelerated Pinterest recommendation model, the number of parameters increased by 100 times, and the user activity increased by 16%
是什么影响了MySQL性能?
力扣练习——56 寻找右区间
Emulate stm32 directly with proteus - the programmer can be completely discarded
快速上手,征服三种不同分布式架构调用方案
老板加薪!看我做的WPF Loading!!!
Gartner reiterates the important value of 'data weaving'
How can an organization judge the success of data governance?
Will SQL and NoSQL eventually converge?
为什么Redis很快
Research on motion capture system for indoor combined positioning technology
C#实战:基于ItextSharp技术标签生成小工具
Intel pushes 20220809 CPU microcode update to patch Intel-SA-00657 security vulnerability
模块九 - 设计电商秒杀系统
微信小程序提交审核历史版本记录从哪里查看
怎么加入自媒体,了解这5种变现模式,让账号快速变现
mysql5.7 installation and deployment - yum installation
【勇敢饭饭,不怕刷题之链表】有序链表的合并
面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?