当前位置:网站首页>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;
}
边栏推荐
- Why can't STM32 download the program
- Postman Interface Association
- [recommended by bloggers] C # generate a good-looking QR code (with source code)
- 学习问题1:127.0.0.1拒绝了我们的访问
- Classes in C #
- Invalid global search in idea/pychar, etc. (win10)
- Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
- When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
- Detailed reading of stereo r-cnn paper -- Experiment: detailed explanation and result analysis
- MySQL主從複制、讀寫分離
猜你喜欢
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
MySQL主從複制、讀寫分離
windows下同时安装mysql5.5和mysql8.0
Did you forget to register or load this tag
Postman uses scripts to modify the values of environment variables
error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead
安装numpy问题总结
Pytorch基础
How to build a new project for keil5mdk (with super detailed drawings)
Windows下安装MongDB教程、Redis教程
随机推荐
Project practice - background employee information management (add, delete, modify, check, login and exit)
Error reporting solution - io UnsupportedOperation: can‘t do nonzero end-relative seeks
NPM an error NPM err code enoent NPM err syscall open
Swagger、Yapi接口管理服务_SE
Julia 1.6 1.7 common problem solving
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
学习问题1:127.0.0.1拒绝了我们的访问
解决安装Failed building wheel for pillow
In the era of DFI dividends, can TGP become a new benchmark for future DFI?
Idea import / export settings file
Neo4j installation tutorial
Ubuntu 20.04 安装 MySQL
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
Postman environment variable settings
MySQL主從複制、讀寫分離
Deoldify project problem - omp:error 15:initializing libiomp5md dll,but found libiomp5md. dll already initialized.
Csdn-nlp: difficulty level classification of blog posts based on skill tree and weak supervised learning (I)
Cookie setting three-day secret free login (run tutorial)
Data dictionary in C #
【博主推荐】C#生成好看的二维码(附源码)