当前位置:网站首页>L2-004 is this a binary search tree? (25 points)
L2-004 is this a binary search tree? (25 points)
2022-07-06 11:25:00 【%xiao Q】
The question :
You are required to judge whether this is the result of traversing a binary search tree or its image in the previous order
analysis :
We can first assume that this is the result of traversing the binary search tree or its image in the previous order , Then we'll see if the following conditions are met :
We can define one 2 A pointer to the tl,tr, Sweep from front to back and from back to front ,tl Look back before , Find the first location larger than the root node ( That is, the first node of the right subtree ),tr Look backwards , Find the first location smaller than the root node ( That is, the last point of the right subtree ), So if it meets tl - tr == 1, Then it is the binary tree that meets the requirements of the topic , About this reason , You can simulate it according to the example .
Reference code :
#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;
// Determine whether it conforms to the binary search tree
if(flag)
{
while(tl <= r && a[tl] < a[l]) tl++; // Find the first of the right subtree
while(tr > l && a[tr] >= a[l]) tr--; // Find the last one to be a subtree
}
// Determine whether it conforms to its image
else
{
while(tl <= r && a[tl] >= a[l]) tl++;
while(tr > l && a[tr] < a[l]) tr--;
}
if(tl - tr != 1) return ; // disqualification , return
dfs(l + 1, tr); // Traverse left subtree
dfs(tl, r); // Traversal right subtree
ans.push_back(a[l]); // Save by subsequent traversal
}
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;
}
边栏推荐
猜你喜欢
随机推荐
vs2019 第一个MFC应用程序
Basic use of redis
Library function -- (continuous update)
QT creator specify editor settings
01 project demand analysis (ordering system)
Ansible实战系列一 _ 入门
Swagger, Yapi interface management service_ SE
Software I2C based on Hal Library
AI benchmark V5 ranking
01项目需求分析 (点餐系统)
Classes in C #
ImportError: libmysqlclient. so. 20: Cannot open shared object file: no such file or directory solution
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)
误删Path变量解决
图片上色项目 —— Deoldify
Learn winpwn (3) -- sEH from scratch
[Thesis Writing] how to write function description of jsp online examination system
Data dictionary in C #
L2-001 紧急救援 (25 分)
[蓝桥杯2021初赛] 砝码称重