CCCC L2-004. 这是二叉搜索树吗?
题意:
一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点,
- 其左子树中所有结点的键值小于该结点的键值;
- 其右子树中所有结点的键值大于等于该结点的键值;
- 其左右子树都是二叉搜索树。
所谓二叉搜索树的“镜像”,即将所有结点的左右子树对换位置后所得到的树。
给定一个整数键值序列,现请你编写程序,判断这是否是对一棵二叉搜索树或其镜像进行前序遍历的结果。
输入格式:
输入的第一行给出正整数N(<=1000)。随后一行给出N个整数键值,其间以空格分隔。
输出格式:
如果输入序列是对一棵二叉搜索树或其镜像进行前序遍历的结果,则首先在一行中输出“YES”,然后在下一行输出该树后序遍历的结果。数字间有1个空格,一行的首尾不得有多余空格。若答案是否,则输出“NO”。
分析:
1、根据二叉搜索树的性质检查前序遍历。
2、二叉搜索树:左结点<右结点,其镜像左结点>右结点
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000 + 10;
int a[MAXN];
bool flag;
vector<int> ans;
void dfs(int L, int R){
if(L > R) return;
int st = L + 1;
int et = R;
if(!flag){
while(st <= R && a[st] < a[L]) ++st;
while(et > L && a[et] >= a[L]) --et;
}
else{
while(st <= R && a[st] >= a[L]) ++st;
while(et > L && a[et] < a[L]) --et;
}
if(et != st - 1) return;
dfs(L + 1, st - 1);
dfs(et + 1, R);
ans.push_back(a[L]);
}
int main(){
int N;
scanf("%d", &N);
for(int i = 0; i < N; ++i){
scanf("%d", &a[i]);
}
flag = false;
dfs(0, N - 1);
if(ans.size() != N){
ans.clear();
flag = true;
dfs(0, N - 1);
}
if(ans.size() == N){
printf("YES\n");
for(int i = 0; i < N; ++i){
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
}
else{
printf("NO\n");
}
return 0;
}
CCCC L2-004. 这是二叉搜索树吗?的更多相关文章
- 天梯 L2 这是二叉搜索树吗?
L2-004 这是二叉搜索树吗? (25 分) 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的键值: 其右子树中所有结点的键值大于等于该结点的 ...
- 有序链表转换二叉搜索树(LeetCode)
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定有序数组: [-10,-3,0, ...
- hdu 3791:二叉搜索树(数据结构,二叉搜索树 BST)
二叉搜索树 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- [LeetCode] Serialize and Deserialize BST 二叉搜索树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
随机推荐
- MAC Matlab 中文乱码
环境:macOS High Sierra 10.13.4 问题:文件中文注释乱码(再次打开文件时) / 控制台输出中文乱码 解决方法: 官网下载补丁(https://ww2.mathworks.cn/ ...
- Vue二次精度随笔(1)
1.button.input标签的disabled属性 该标签可以控制按钮是否可用,如果他的值为以上几种的话,则他都不会在标签上渲染出这个属性,一旦这个属性出现的话,就说明他是禁用的 2.移除动态绑定 ...
- 仿照Android标准API写的各种形式的弹出框
strings.xml: <?xml version="1.0" encoding="utf-8"?> <resources> < ...
- IOS UIPanGestureRecognizer手势使用及识别状态UIGestureRecognizerState
UIGestureRecognizerState -- 手势识别器状态 1.先来看官方文档 定义UIGestureRecognizer.h 英文: typedef NS_ENUM(NSInteger, ...
- 防止SQL注入的登录页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/T ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片:响应式图片
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- greenplum 存储过程
参考: https://docs.pivotal.io/search?q=function 官网 https://www.cnblogs.com/greenZ/p/8722081.html htt ...
- 小程序真机上报错 for developer: some selectors are not allowed in component wxss , including tag name selectors, id selectors, and attribute selectors
for developer: some selectors are not allowed in component wxss , including tag name selectors, id s ...
- 【Winform】键 盘 事 件
private void richTextBox1_KeyPress(object sender, KeyPressEventArgs e) { , (, (, (, ( }; //回车 Backsp ...
- 【LeetCode】找出所有数组中消失的数字
[问题] 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次.找到所有在 [1, n] 范围之间没有出现在数组中的数字. ...