No.1119

题目:由前序后序二叉树序列,推中序,判断是否唯一后输出一组中序序列

思路:前序从前向后找,后序从后向前找,观察正反样例可知,前后序树不唯一在于单一子树是否为左右子树。

判断特征:通过查找后序序列中最后一个结点的前一个在先序中的位置,来确定是否可以划分左右孩子,如果不能,  就将其划分为右孩子(或左孩子),递归建树。

中序遍历输出。

#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)【二叉树遍历】的更多相关文章

  1. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  2. HDU 1710 Binary Tree Traversals(二叉树遍历)

    传送门 Description A binary tree is a finite set of vertices that is either empty or consists of a root ...

  3. PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历

    题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...

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

    PAT甲级1119. Pre- and Post-order Traversals 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二进制树可以通过给定的一对后序和顺序遍历序列来确定,也可以通 ...

  5. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  6. [二叉树建树]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 ...

  7. 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 ...

  8. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  9. 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 ...

随机推荐

  1. bzoj4199:NOI2015D2T2品酒大会(SAM版)

    SAM感觉写起来比SA更直观(?) #include <iostream> #include <cstdio> #include <cstring> #includ ...

  2. Java基础-一个java文件多个类的问题

    一个.java文件当然可以包括多个类.但这些类有一个特殊的类与其它的不同,,这个类是带public 属性的类.一个.java类文件中仅有一个public属性的类.而且这个类与文件名相同.

  3. Linux解压,压缩小总结

    linux下打包与解压的三种命令 最近在读<鸟歌的Linux私房菜基础篇>,想着总结一下所读知识,有益于理解. Linux下常用的命令有三种 gzip,zcat(用于zip,gzip等) ...

  4. Twemproxy 缓存代理服务器

    Twemproxy 缓存代理服务器 Twemproxy 概述 Twemproxy(又称为nutcracker)是一个轻量级的Redis和Memcached代理,主要用来减少对后端缓存服务器的连接数.T ...

  5. MySQL中find_in_set()和in的区别

    弄个测试表来说明两者的区别 CREATE TABLE `test` ( `id` int(8) NOT NULL auto_increment, `name` varchar(255) NOT NUL ...

  6. 【总结】虚拟机VirtualBox各种使用技巧

    作为个人学习研究,VirtualBox是首选,它是Oracle下免费的.开源.跨平台的一款虚拟机软件,小巧.实用,一点也不逊于商业版的VMware Workstation. VirtualBox官网: ...

  7. 进阶系列一【绝对干货】---SQL语句执行效率优化

    1.尽量适用联接查询来取代子查询 2.如果要用子查询,用EXISTS替代IN.用NOT EXISTS替代NOT IN,因为EXISTS引入的子查询只是测试是否存在符合子查询中指定条件的行,效率较高.无 ...

  8. LINUX DNS解析的3种修改方法~

    1.HOST 本地DNS解析 vi /etc/hosts 添加规则 例如: 223.231.234.33 www.baidu.com 2.网卡配置文件DNS服务地址  vi /etc/sysconfi ...

  9. BZOJ 4725: [POI2017]Reprezentacje ró?nicowe

    Description 一个数列. \(a_1=1,a_2=2\) 当 \(n>2\) 时 \[a_n = \{  \begin {matrix} 2a_{n-1},\text{n is an ...

  10. 利用Python自动生成暴力破解的字典

    Python是一款非常强大的语言.用于测试时它非常有效,因此Python越来越受到欢迎. 因此,在此次教程中我将聊一聊如何在Python中生成字典,并将它用于任何你想要的用途. 前提要求 1,Pyth ...