【PAT】【Advanced Level】1043. Is It a Binary Search Tree (25)

2024-06-17 04:32

本文主要是介绍【PAT】【Advanced Level】1043. Is It a Binary Search Tree (25),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1043. Is It a Binary Search Tree (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.

Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in a line "YES" if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or "NO" if not. Then if the answer is "YES", print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

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
原题链接:

https://www.patest.cn/contests/pat-a-practise/1043

https://www.nowcoder.com/pat/5/problem/4082

思路:

重新建树并遍历

注意细节处理

CODE:

#include<iostream>
#include<cstring>
#include<string>
#define N 1001using namespace std;typedef struct S
{int val;int ls,rs;	
};
S no[N];bool dfs1(int l,int r)
{//cout<<l<<" "<<r<<endl;if (l==r) return true;int f=-1;bool fla=0;for (int i=l+1;i<=r;i++){if (no[i].val>=no[l].val){fla=1;break;}f=i;}//cout<<f<<endl;bool pd=1;if (fla==1){for (int i=max(f+1,l+1);i<=r;i++) //max 不可忘 if (no[i].val<no[l].val)return false;no[l].rs=max(f+1,l+1);pd=pd&dfs1(max(f+1,l+1),r);}if (f!=-1){no[l].ls=l+1;pd=pd&dfs1(l+1,f);}	return pd;
}bool dfs2(int l,int r)
{//cout<<l<<" "<<r<<endl;if (l==r) return true;int f=-1;bool fla=0;for (int i=l+1;i<=r;i++){if (no[i].val<no[l].val){fla=1;break;}f=i;}//cout<<f<<" "<<fla<<endl;bool pd=1;if (fla==1){for (int i=max(f+1,l+1);i<=r;i++)	//max 不可忘 if (no[i].val>no[l].val)return false;no[l].rs=max(f+1,l+1);pd=pd&dfs2(max(f+1,l+1),r);}if (f!=-1){no[l].ls=l+1;pd=pd&dfs2(l+1,f);}	return pd;
}void pri(int n)
{if (no[n].ls!=-1){pri(no[n].ls);}if (no[n].rs!=-1){pri(no[n].rs);}cout<<no[n].val;if (n!=1) cout<<" ";
}
int main()
{int n;cin>>n;for (int i=1;i<=n;i++){S t;int v;cin>>v;t.val=v;t.ls=t.rs=-1;no[i]=t;}bool fl;//cout<<n<<endl;//cout<<no[1].val<<" "<<no[2].val<<endl;if (n==1 || no[1].val>no[2].val){//cout<<no[1].val<<" "<<no[2].val<<endl;fl=dfs1(1,n);}	else{fl=dfs2(1,n);}//cout<<fl<<endl;if (fl==0){cout<<"NO";}else{cout<<"YES"<<endl;pri(1);}return 0;
}



这篇关于【PAT】【Advanced Level】1043. Is It a Binary Search Tree (25)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1068483

相关文章

HTML5 搜索框Search Box详解

《HTML5搜索框SearchBox详解》HTML5的搜索框是一个强大的工具,能够有效提升用户体验,通过结合自动补全功能和适当的样式,可以创建出既美观又实用的搜索界面,这篇文章给大家介绍HTML5... html5 搜索框(Search Box)详解搜索框是一个用于输入查询内容的控件,通常用于网站或应用程

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

五大特性引领创新! 深度操作系统 deepin 25 Preview预览版发布

《五大特性引领创新!深度操作系统deepin25Preview预览版发布》今日,深度操作系统正式推出deepin25Preview版本,该版本集成了五大核心特性:磐石系统、全新DDE、Tr... 深度操作系统今日发布了 deepin 25 Preview,新版本囊括五大特性:磐石系统、全新 DDE、Tree

uva 575 Skew Binary(位运算)

求第一个以(2^(k+1)-1)为进制的数。 数据不大,可以直接搞。 代码: #include <stdio.h>#include <string.h>const int maxn = 100 + 5;int main(){char num[maxn];while (scanf("%s", num) == 1){if (num[0] == '0')break;int len =

AI基础 L9 Local Search II 局部搜索

Local Beam search 对于当前的所有k个状态,生成它们的所有可能后继状态。 检查生成的后继状态中是否有任何状态是解决方案。 如果所有后继状态都不是解决方案,则从所有后继状态中选择k个最佳状态。 当达到预设的迭代次数或满足某个终止条件时,算法停止。 — Choose k successors randomly, biased towards good ones — Close

树(Tree)——《啊哈!算法》

树 什么是树?树是一种特殊的图,不包含回路的连通无向树。 正因为树有着“不包含回路”这个特点,所以树就被赋予了很多特性。 一棵树中的任意两个结点有且仅有唯一的一条路径连通。一棵树如果有n个结点,那么它一定恰好有n-1条边。在一棵树中加一条边将会构成一个回路。 一棵树有且只有一个根结点。 没有父结点的结点称为根结点(祖先)。没有子结点的结点称为叶结点。如果一个结点既不是根结点也不是叶

【JavaScript】LeetCode:21-25

文章目录 21 最大子数组和22 合并区间23 轮转数组24 除自身以外数组的乘积25 缺失的第一个正数 21 最大子数组和 贪心 / 动态规划贪心:连续和(count)< 0时,放弃当前起点的连续和,将下一个数作为新起点,这里提供使用贪心算法解决本题的代码。动态规划:dp[i]:以nums[i]为结尾的最长连续子序列(子数组)和。 dp[i] = max(dp[i - 1]

JavaScript正则表达式六大利器:`test`、`exec`、`match`、`matchAll`、`search`与`replace`详解及对比

在JavaScript中,正则表达式(Regular Expression)是一种用于文本搜索、替换、匹配和验证的强大工具。本文将深入解析与正则表达式相关的几个主要执行方法:test、exec、match、matchAll、search和replace,并对它们进行对比,帮助开发者更好地理解这些方法的使用场景和差异。 正则表达式基础 在深入解析方法之前,先简要回顾一下正则表达式的基础知识。正则

插件maven-search:Maven导入依赖时,使用插件maven-search拷贝需要的依赖的GAV

然后粘贴: <dependency>    <groupId>mysql</groupId>    <artifactId>mysql-connector-java</artifactId>    <version>8.0.26</version> </dependency>

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro