PAT甲级1119. Pre- and Post-order Traversals

题意:

假设二叉树中的所有键都是不同的正整数。一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通过预序和顺序遍历序列来确定。然而,如果仅给出了后序和预序遍历序列,则相应的树可能不再是唯一的。

现在给出一对postorder和preorder遍历序列,你应该输出树的相应的顺序遍历序列。如果树不是唯一的,只需输出任何一个树。

输入规格:

每个输入文件包含一个测试用例。对于每种情况,第一行给出正整数N(<= 30),

二叉树中的节点总数。第二行给出了预订序列,第三行给出了后序序列。一行中的所有数字都以空格分隔。

输出规格:

对于每个测试用例,如果树是唯一的,则第一个printf为“是”,否则为“否”。

然后在下一行打印相应二叉树的顺序遍历序列。如果解决方案不是唯一的,任何答案都可以。保证至少有一个解决方案存在。一行中的所有数字必须只有一个空格分开,并且行尾不能有额外的空格。

思路:

二叉树的构造和遍历。题目感觉比较有点毛病。1A总让我感觉怪怪的。

ac代码:

C++

// pat1119.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" #include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<stdio.h>
#include<map>
#include<cmath>
#include<unordered_map>
#include<unordered_set> using namespace std; int n;
int pre[31];
int post[31];
int in[31]; struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : val(x) , left(NULL) , right(NULL) {}
}; bool flag = true; TreeNode* build(int prel, int prer, int postl, int postr)
{
if (prel > prer || postl > postr) return NULL;
TreeNode* root = new TreeNode(pre[prel]);
prel++;
postr--;
if (prel > prer || postl > postr) return root; int temp = postl;
while (temp <= postr && post[temp] != pre[prel]) temp++;
root->left = build(prel, prel + temp - postl, postl, temp); if (temp == postr)
{
flag = false;
return root;
} prel = prel + temp - postl + 1;
postl = temp + 1;
temp = postl;
while (temp < postr && post[temp] != pre[prel]) temp++;
root->right = build(prel, prel + temp - postl, postl, temp); return root;
} void inorder(TreeNode* root)
{
if (!root) return;
static int pos = 0; inorder(root->left);
in[pos++] = root->val;
inorder(root->right);
} int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++) scanf("%d", &pre[i]);
for (int i = 0; i < n; i++) scanf("%d", &post[i]); TreeNode* root = build(0, n - 1, 0, n - 1);
inorder(root); if (flag) printf("Yes\n");
else printf("No\n");
for (int i = 0; i < n - 1; i++)
printf("%d ", in[i]);
printf("%d\n", in[n - 1]); return 0;
}

PAT甲级1119. Pre- and Post-order Traversals的更多相关文章

  1. PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca

    给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...

  2. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  3. PAT甲级考前整理(2019年3月备考)之一

       转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...

  4. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  5. PAT甲级1123. Is It a Complete AVL Tree

    PAT甲级1123. Is It a Complete AVL Tree 题意: 在AVL树中,任何节点的两个子树的高度最多有一个;如果在任何时候它们不同于一个,则重新平衡来恢复此属性.图1-4说明了 ...

  6. PAT甲级1114. Family Property

    PAT甲级1114. Family Property 题意: 这一次,你应该帮我们收集家族财产的数据.鉴于每个人的家庭成员和他/她自己的名字的房地产(房产)信息,我们需要知道每个家庭的规模,以及他们的 ...

  7. PAT甲级1107. Social Clusters

    PAT甲级1107. Social Clusters 题意: 当在社交网络上注册时,您总是被要求指定您的爱好,以便找到一些具有相同兴趣的潜在朋友.一个"社会群体"是一群拥有一些共同 ...

  8. PAT甲级1045. Favorite Color Stripe

    PAT甲级1045. Favorite Color Stripe 题意: 伊娃正在试图让自己的颜色条纹从一个给定的.她希望通过剪掉那些不必要的部分,将其余的部分缝合在一起,形成她最喜欢的颜色条纹,以保 ...

  9. PAT甲级1021. Deepest Root

    PAT甲级1021. Deepest Root 题意: 连接和非循环的图可以被认为是一棵树.树的高度取决于所选的根.现在你应该找到导致最高树的根.这样的根称为最深根. 输入规格: 每个输入文件包含一个 ...

随机推荐

  1. redis从入门到放弃 -> 部署方案

    单点部署方案 环境准备: [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [root@ ...

  2. MySQL 四种链接

    1.内联接 INNER JOIN(典型的联接运算,使用像 =  或 <> 之类的比较运算符).包括相等联接和自然联接.     内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行. ...

  3. 一、springboot入门

    构建spring boot工程一般采用两种方式 gradle .maven maven方式 pom.xml spring-boot-starter:核心模块,包括自动配置支持.日志和YAML spri ...

  4. docker强制关闭命令

    删除容器: 优雅的关闭容器:docker stop  容器id/容器名字 强制关闭容器:docker kill 容器id/容器名字 删除镜像: docker rmi 容器id/容器名字

  5. 3.Springboot之修改启动时的默认图案Banner

    一.SpringBoot的默认启动图案 在SpringBoot启动的时候,默认的会展示出一个spring的logo,这个图案我们用户是可以自定义的 二.自定义启动图案 方法一: Application ...

  6. SQLServer判断指定列的默认值是否存在,并修改默认值

    SQLServer判断指定列的默认值是否存在,并修改默认值 2008年10月21日 星期二 下午 12:08 if exists(select A.name as DefaultName,B.name ...

  7. Sql Server2005 Transact-SQL 新兵器学习总结之-排名函数

    Transact-SQL提供了4个排名函数: rand() , dense_rand() , row_number() , ntile() 下面是对这4个函数的解释:rank() 返回结果集的分区内每 ...

  8. Spring介绍及配置(XML文件配置和注解配置)

    本节内容: Spring介绍 Spring搭建 Spring概念 Spring配置讲解 使用注解配置Spring 一.Spring介绍 1. 什么是Spring Spring是一个开源框架,Sprin ...

  9. gym 100531 三维几何+搜索

    精度有点毒, 其实可以不用double, 因为A, B必定在其中一个在三角形上,可以投影到只有x,y轴的地方叉积比较. #include<bits/stdc++.h> #define LL ...

  10. cent7.0 mysql 修改端口

    如何查看mysql 默认端口号和修改端口号 2015-03-19 17:42:18 1. 登录mysql [root@test /]# mysql -u root -p Enter password: ...