当前位置:网站首页>L2-004 这是二叉搜索树吗? (25 分)
L2-004 这是二叉搜索树吗? (25 分)
2022-07-06 09:14:00 【%xiao Q】
题意:
要求你判断这是否是一颗二叉搜索树或其镜像按前序遍历的结果
分析:
我们可以先假设这个是二叉搜索树或其镜像按前序遍历的结果,然后在看是否符合下面的条件:
我们可以定义一个2个指针tl,tr,分别从前往后扫和从后往前的扫,tl从前往后找,找第一个比根节点节点大的位置(即右子树的第一个节点),tr从后往前找,找第一个比根节点小的位置(即右子树最后一个点),所以如果符合tl - tr == 1,那么就是符合题目要求的二叉树,关于这个原因,你们可以按样例来模拟一下。
参考代码:
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <algorithm>
#include <unordered_map>
#define LL long long
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define reps(i, a, b) for(int i = a; i < b; i++)
#define pre(i, a, b) for(int i = b; i >= a; i--)
using namespace std;
const int N = 1010;
int a[N];
vector<int> ans;
bool flag = true;
void dfs(int l, int r)
{
if(l > r) return ;
int tl = l + 1, tr = r;
// 判断是否符合二叉搜索树
if(flag)
{
while(tl <= r && a[tl] < a[l]) tl++; //找右子树的第一个
while(tr > l && a[tr] >= a[l]) tr--; //找做子树的最后一个
}
// 判断是否符合其镜像
else
{
while(tl <= r && a[tl] >= a[l]) tl++;
while(tr > l && a[tr] < a[l]) tr--;
}
if(tl - tr != 1) return ; //不符合条件,返回
dfs(l + 1, tr); //遍历左子树
dfs(tl, r); //遍历右子树
ans.push_back(a[l]); //按后续遍历来存
}
int main()
{
int n;
cin >> n;
rep(i, 1, n) cin >> a[i];
dfs(1, n);
if(ans.size() != n)
{
flag = false;
ans.clear();
dfs(1, n);
}
if(ans.size() != n) puts("NO");
else
{
puts("YES");
rep(i, 0, n - 1)
{
if(i) cout << " ";
cout << ans[i];
}
cout << endl;
}
return 0;
}
边栏推荐
- 解决:log4j:WARN Please initialize the log4j system properly.
- Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
- Postman Interface Association
- AcWing 179. Factorial decomposition problem solution
- 1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
- Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
- 打开浏览器的同时会在主页外同时打开芒果TV,抖音等网站
- 虚拟机Ping通主机,主机Ping不通虚拟机
- Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
- 解决安装Failed building wheel for pillow
猜你喜欢

Use dapr to shorten software development cycle and improve production efficiency

Request object and response object analysis
![[number theory] divisor](/img/ec/036d7e76cc566c08d336444f2898e1.jpg)
[number theory] divisor

图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path

Django running error: error loading mysqldb module solution

【博主推荐】C# Winform定时发送邮箱(附源码)

QT creator create button

LeetCode #461 汉明距离
![[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP](/img/7d/8cbbd2f328a10808319458a96fa5ec.jpg)
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP

机器学习笔记-Week02-卷积神经网络
随机推荐
Did you forget to register or load this tag
AcWing 1294. Cherry Blossom explanation
Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
虚拟机Ping通主机,主机Ping不通虚拟机
How to configure flymcu (STM32 serial port download software) is shown in super detail
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
Unable to call numpy in pycharm, with an error modulenotfounderror: no module named 'numpy‘
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)
Esp8266 at+cipstart= "", "", 8080 error closed ultimate solution
Solution: log4j:warn please initialize the log4j system properly
Leetcode 461 Hamming distance
Punctual atom stm32f103zet6 download serial port pin
Basic use of redis
[BMZCTF-pwn] 11-pwn111111
Did you forget to register or load this tag 报错解决方法
[C language foundation] 04 judgment and circulation
Invalid default value for 'create appears when importing SQL_ Time 'error reporting solution
Antlr4 uses keywords as identifiers
记某公司面试算法题:查找一个有序数组某个数字出现的次数