当前位置:网站首页>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;
}
边栏推荐
- CPU多级缓存与缓存一致性
- 力扣练习——59 从二叉搜索树到更大和树
- 今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板
- Where can I view the version record of WeChat applet submission review history?
- 从产品角度看 L2 应用:为什么说这是一个游乐场?
- 【电商运营】你真的了解社交媒体营销(SMM)吗?
- 什么是幂等性?四种接口幂等性方案详解!
- 使用.NET简单实现一个Redis的高性能克隆版(六)
- 基于UiAutomator2+PageObject模式开展APP自动化测试实战
- Short video software development - how to break the platform homogenization
猜你喜欢
Do self-media monthly income tens of thousands?Several self-media tools that bloggers are using
In August the DB list latest scores - database Engines
第2章-矩阵及其运算-矩阵运算(2)
ISO9001在讲什么?过程方法和风险思维
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
Store limited time seckill function system
机器学习之暴力调参案例
一文带你搞懂中断按键驱动程序之poll机制
HCIP ---- VLAN
Several small projects that I have open sourced over the years
随机推荐
不止跑路,拯救误操作rm -rf /*的小伙儿
使用.NET简单实现一个Redis的高性能克隆版(六)
blocking non-blocking poll mechanism asynchronous
负载均衡原理分析与源码解读
Codeforces 814 C. An impassioned circulation of affection (dp)
阻塞 非阻塞 poll机制 异步
POJ 3101 Astronomy (数学)
三个绘图工具类详解Paint(画笔)Canvas(画布)Path(路径)
StoneDB 文档捉虫活动第一季
GPU accelerated Pinterest recommendation model, the number of parameters increased by 100 times, and the user activity increased by 16%
The impact of development mode on testing
In August the DB list latest scores - database Engines
【TypeScript】接口类型与类型别名:这两者的用法与区别分别是什么?
建校仅11年就入选“双一流” ,这所高校是凭什么做到的?
什么是幂等性?四种接口幂等性方案详解!
HDU 1520 Anniversary party (树型dp)
力扣练习——63 找到字符串中所有字母异位词
POJ 1026 Cipher (Permutation Groups)
软件架构简介
STM32封装ESP8266一键配置函数:实现实现AP模式和STA模式切换、服务器与客户端创建