当前位置:网站首页>Ladder race -- l2-004 is this a binary search tree? (25 points) (recursive)
Ladder race -- l2-004 is this a binary search tree? (25 points) (recursive)
2022-04-22 14:22:00 【wowon~】
L2-004 Is this a binary search tree ? (25 branch )
A binary search tree can be recursively defined as a binary tree with the following properties : For any node ,
- The key value of all nodes in the left subtree is less than that of the node ;
- The key value of all nodes in the right subtree is greater than or equal to the key value of the node ;
- Its left and right subtrees are binary search trees .
The so-called binary search tree “ Mirror image ”, That is, the tree obtained by transposing the left and right subtrees of all nodes .
Given a sequence of integer key values , Now please write a program , Judge whether this is the result of preorder traversal of a binary search tree or its image .
Input format :
The first line of input gives a positive integer N(≤1000). The next line shows N An integer key value , Separated by spaces .
Output format :
If the input sequence is the result of preorder traversal of a binary search tree or its image , First output... On one line YES , And then output the result of traversing the tree in the next line . Between the numbers 1 A space , There must be no extra spaces at the beginning and end of a line . If the answer is no , The output NO.
sample input 1:
7
8 6 5 7 10 8 11
sample output 1:
YES
5 7 6 8 11 10 8
sample input 2:
7
8 10 11 8 6 7 5
sample output 2:
YES
11 8 10 7 5 6 8
sample input 3:
7
8 6 8 5 10 9 11
sample output 3:
NO
about 8 6 5 7 10 8 11
that L=1,R=7 ( Because the subscript is from 1 Start )
get(1,7)
l=2,r=7;
Binary search tree ( Non mirror ) in :l Finally 5 r by 4 So continue recursion get( L+1,r); get(l,R) ; hold a[L] Put in ans Inside
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[1010];
vector<int>ans;
int n;
int f;
void get(int L,int R)
{
if(L>R)return;// Do not meet the conditions directly return
int l=L+1,r=R;
if(f)// Binary search tree
{
while(l <= R && a[l] < a[L]) l++;//l Move to the right to find the first one greater than or equal to a[L] Number of numbers
while(r > L && a[r] >= a[L]) r--;//r Find the first move to the left a[L] Number of numbers
}
else// Mirrored binary search tree
{
while(l <= R && a[l] >= a[L]) l++;//l Move to the right to find the first one less than a[L] Number of numbers
while(r > L && a[r] < a[L]) r--;
}
if(l-r!=1)return;// Unqualified
get(L+1,r);
get(l,R);
ans.push_back(a[L]);
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i];
get(1,n);// A binary search tree is the result of preorder traversal
if(ans.size() != n)
{
f = 1;
ans.clear();
get(1,n);// The result of preorder traversal of a binary search tree
}
if(ans.size() != n) cout<<"NO"<<endl;
else
{
cout<<"YES"<<endl<<ans[0];
for(int i=1;i<ans.size();i++)
cout<<" "<<ans[i];
cout << endl;
}
return 0;
}
版权声明
本文为[wowon~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221416432909.html
边栏推荐
- 天梯赛--L2-004 这是二叉搜索树吗? (25 分)(递归)
- 双指针头尾指针 167、|345、680、15、16、18、11、42
- 2D conversion (move: translate, rotate: rotate, scale: scale, 2D conversion synthesis)
- 深入理解Condition
- Completion of minmao e-commerce in 10 days - Implementation of user module (2nd day)
- Double pointer in the same direction, double pointer, sliding window 3, 594, |27, 26, 80, 83, 82, 611, 187, 643, 674, 209, 438, 567, 424, 76, 30
- In February, I relied on this PDF document to interview bat. Unexpectedly, I received five offers
- 运行npm install命令的时候会发生什么?
- HashTable哈希表练习查找插入删除217、349 、202、287、290、532、205、128
- 3. fiddler证书安装和抓取hettps设置
猜你喜欢

北斗gps卫星时间同步装置(卫星时钟)在广电系统的应用

Deep understanding of condition

图的遍历 深度优先DFS 广度优先BFS

Redis connection tool cannot connect to redis in docker

MariaDB is configured as master-slave (dual master mode) to each other

2. Flddler response shows the solution to the problem of garbled code

我为什么那么爱用飞项做任务管理

MapReduce高级应用——全排序和二次排序

关于半导体的8个奇特事实

Blocking queue-
随机推荐
gradle 引用平级项目
Binarytree practice binary tree serialization and deserialization 606, 331, 652, 297
字节跳动的面试分享,为了拿下这个offer鬼知道我经历了什么
TopK
LeetCode_63 不同路径 II
leetcode:215. 数组中的第K个最大元素
天梯赛--L2-003 月饼 (25 分)
logcat的使用
interrupted()和isInterrupted()详述,百万数据分页查询的方法及其优化方式
深入剖析阻塞队列BlockingQueue (详解ArrayBlockingQueue和LinkedBlockingQueue及其应用)
Advanced multithreading
ArcEngine符号相关
Activity preview | on April 23, a number of wonderful openmldb sharing came, which lived up to the good time of the weekend!
awk命令
spark代码 spark-submit提交yarn-cluster模式
双指针||有序数组去重 排序链表移除元素 26、27、83
Binary Tree练习二叉树遍历递归257、100、222、101、226、437、563、617、572、543、|687
Pratique de l'arbre binaire itération récursive de l'arbre binaire 257, 100, 222, 101, 226, 437, 563, 617, 572, 543, | 687
@Resource与构造函数踩坑
深入剖析volatile原理