PAT A 1119. Pre- and Post-order Traversals (30)【二叉树遍历】
题目:由前序后序二叉树序列,推中序,判断是否唯一后输出一组中序序列
思路:前序从前向后找,后序从后向前找,观察正反样例可知,前后序树不唯一在于单一子树是否为左右子树。
判断特征:通过查找后序序列中最后一个结点的前一个在先序中的位置,来确定是否可以划分左右孩子,如果不能, 就将其划分为右孩子(或左孩子),递归建树。
中序遍历输出。
#include <iostream>
using namespace std;
const int maxn = 31; int n, index = 0;
int pre[maxn], post[maxn];
bool flag = true; struct Node {
int data;
Node *lchild, *rchild;
} *root; Node *create(int preL, int preR, int postL, int postR)
{
if (preL > preR) return NULL;
Node *node = new Node;
node->data = pre[preL];
node->lchild = NULL;
node->rchild = NULL;
if (preL == preR)
return node;
int k = 0;
for (k = preL + 1; k <= preR; k++)
{
if (pre[k] == post[postR - 1]) break;
}
if (k - preL > 1)
{
node->lchild = create(preL + 1, k - 1, postL, postL + k - preL - 2);
node->rchild = create(k, preR, postL + k - preL - 1, postR - 1);
}
else
{
flag = false;
node->rchild = create(k, preR, postL + k - preL - 1, postR - 1);
}
return node;
} void inOrder(Node *node)
{
if (node == NULL) return;
inOrder(node->lchild);
if (index < n - 1)
cout << node->data << " ";
else cout << node->data << endl;
index++;
inOrder(node->rchild);
} int main()
{
cin >> n;
for (int i = 0; i < n; ++i) cin >> pre[i];
for (int i = 0; i < n; ++i) cin >> post[i];
root = create(0, n - 1, 0, n - 1);
if (flag) cout << "Yes\n";
else cout << "No\n";
inOrder(root);
return 0;
}
PAT A 1119. Pre- and Post-order Traversals (30)【二叉树遍历】的更多相关文章
- HDU 1710 Binary Tree Traversals (二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU 1710 Binary Tree Traversals(二叉树遍历)
传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...
- PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历
题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...
- PAT甲级1119. Pre- and Post-order Traversals
PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- [二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)
1119. Pre- and Post-order Traversals (30) Suppose that all the keys in a binary tree are distinct po ...
- Construct a tree from Inorder and Level order traversals
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is a ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT 1119 Pre- and Post-order Traversals [二叉树遍历][难]
1119 Pre- and Post-order Traversals (30 分) Suppose that all the keys in a binary tree are distinct p ...
随机推荐
- couldn't open file: data/coco.names
在ubuntu下配置yolo(v2)的时候,编译了源码后,尝试运行demo: ./darknet detect cfg/yolo.cfg yolo.weights data/dog.jpg 结果报错提 ...
- BZOJ2728: [HNOI2012]与非
线性基乱搞,非正解= = #include<cstdio> int n,m; typedef long long ll; ll l[60],j,s,t; void up(ll& i ...
- web前端基础知识-(一)html基本操作
1. HTML概述 HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标记).相当于定义统一的一套规则,大家都来遵守他,这样 ...
- centos ab命令安装
yum install apr-util -ymkdir abcd abyum -y install yum-utils -yyumdownloader httpd yum localinstall ...
- The RAII Programming Idiom
https://www.hackcraft.net/raii/ https://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization
- 苹果微信下载 iOS微信各版本列表
微信在不断地更新迭代,ios微信下载点击这里立即开始(手机电脑都可以,电脑端要安装iTunes),每个版本都放出一些新的功能或修复相关错误,详情可以点击下面的版本链接进行查看.(这里有Android微 ...
- Linux下查看操作系统信息、内存情况及cpu信息:cpu个数、核心数、线程数
1.查看物理CPU的个数 [root@MysqlCluster01 ~]# cat /proc/cpuinfo |grep "physical id"|sort |uniq|wc ...
- Linux mkdir 创建文件夹命令
介绍: 该命令创建指定的目录名,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录 语法: mkdir [-m] [-p] 目录名 选项介绍: -m: 对新建目录设置 ...
- tornado 第二种路由方法(装饰器)
#!/usr/bin/env python # _*_coding:utf-8 _*_ import tornado.ioloop import tornado.web application = t ...
- PHP写的异步高并发服务器,基于libevent
PHP写的异步高并发服务器,基于libevent 博客分类: PHP PHPFPSocketLinuxQQ 本文章于2013年11月修改. swoole已使用C重写作为PHP扩展来运行.项目地址:h ...