1119. Pre- and Post-order Traversals (30)

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first printf in a line "Yes" if the tree is unique, or "No" if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input 1:

7
1 2 3 4 6 7 5
2 6 7 4 5 3 1

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4

分析:目前,关于二叉树的遍历算法,有前序和中序,后序和中序,层序和中序,这三种组合都是可以唯一确定一颗二叉树的,目前都有用代码实现。其中,前序和中序建树和后序和中序建树两种方法都是想办法分出左右子树,然后对左右字数进行递归建树。层序和中序也可以用递归实现,但是目前我只用了非递归的实现方法。关于今天的建树方法,使用的是二叉树的前序遍历和后序遍历。但是我们知道,仅有前序遍历和后序遍历是没法确定一颗二叉树的。这里,前序遍历和后序遍历在某些调剂下可以唯一确定一颗二叉树。但这里不做这个要求。题目的要求是当产生歧义是可随意建立一颗满足前序和后序遍历的二叉树即可。

  目前已经碰到了二叉树建树的大多数情况,下次有时间稍微总结一下。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std; vector<int > pre,post,in; struct Node
{
int data;
Node *l,*r;
}; bool flag=true; void creat(Node * & root,int preL,int preR,int postL,int postR)
{
if(preL==preR)
{
root=new Node;
root->data=pre[preL];
root->l=root->r=NULL;
return ;
}
root=new Node;
root->data=pre[preL];
root->l=root->r=NULL;
int i,j;
for(i=postL;i<=postR;i++)
{
if(post[i]==pre[preL+])
{
break;
}
}
int leftNum=i-postL;
if(post[i]==post[postR-])//不确定的条件,无法区分是左子树还是右子树
{
flag=;
creat(root->l,preL+,preR,postL,postR-);
}
else
{
creat(root->l,preL+,preL++leftNum,postL,postL+leftNum);
creat(root->r,preL+leftNum+,preR,postL+leftNum+,postR-);
}
} vector<int> ans; void inOrder(Node * root)
{
if(root!=NULL)
{
inOrder(root->l);
ans.push_back(root->data);
inOrder(root->r);
}
} void outans()
{
for(int i=;i<ans.size();i++)
{
if(i>) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
} int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
{
int k;
scanf("%d",&k);
pre.push_back(k);
}
for(int i=;i<n;i++)
{
int k;
scanf("%d",&k);
post.push_back(k);
}
Node * root=NULL;
creat(root,,n-,,n-);
inOrder(root);
if(flag==true) printf("Yes\n");
else printf("No\n");
outans();
return ;
}

[二叉树建树]1119. Pre- and Post-order Traversals (30) (前序和后序遍历建立二叉树)的更多相关文章

  1. PAT-1119(Pre- and Post-order Traversals)+前序和后序遍历确定二叉树+判断二叉树是否唯一

    Pre- and Post-order Traversals PAT-1119 这题难度较大,主要需要考虑如何实现根据前序遍历和后序遍历来确定一颗二叉树 一篇好的文章: 题解 import java. ...

  2. [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  3. [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  4. LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  6. [LeetCode] 889. Construct Binary Tree from Preorder and Postorder Traversal 由先序和后序遍历建立二叉树

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  7. 笔试算法题(36):寻找一棵二叉树中最远节点的距离 & 根据二叉树的前序和后序遍历重建二叉树

    出题:求二叉树中距离最远的两个节点之间的距离,此处的距离定义为节点之间相隔的边数: 分析: 最远距离maxDis可能并不经过树的root节点,而树中的每一个节点都可能成为最远距离经过的子树的根节点:所 ...

  8. [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树

    Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...

  9. [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. Redis--位图BitMap

    一.BitMap是什么 通过一个bit位来表示某个元素对应的值或者状态,其中的key就是对应元素本身,value对应0或1,我们知道8个bit可以组成一个Byte,所以bitmap本身会极大的节省储存 ...

  2. Scala(三):类

    类:Class 1.简单类和无参方法 2.带getter和setter属性 3.只带getter属性 4.对象私有字段 5.Bean属性 6.辅助构造器 7.主构造器 8.嵌套类 1.简单类和无参方法 ...

  3. ExcelVBA实现一键生成word文字报告及批量操作[原创]

    在很多工作中,经常需要写一些类似的报告,使用同一个模板,只是里面的数据不同,人工操作工程量大且容易出错,如果能用程序直接实现可以省去不少麻烦. 本文使用ExcelVBA实现,主要思路是使用word邮件 ...

  4. spring boot 资料

    http://412887952-qq-com.iteye.com/blog/2344171 http://study.163.com/course/courseMain.htm?courseId=1 ...

  5. C++多线程,互斥,同步

    同步和互斥 当有多个线程的时候,经常需要去同步这些线程以访问同一个数据或资源.例如,假设有一个程序,其中一个线程用于把文件读到内存,而另一个线程用于统计文件中的字符数.当然,在把整个文件调入内存之前, ...

  6. 4-[多进程]-互斥锁、Queue队列、生产者消费者

    1.互斥锁 (1)为什么需要互斥锁 进程之间数据不共享,但是共享同一套文件系统,所以访问同一个文件,或同一个打印终端,是没有问题的, 而共享带来的是竞争,竞争带来的结果就是错乱,如下 #并发运行,效率 ...

  7. CentOS7 msmtp+mutt发送邮件

    一.安装软件 # msmtp软件各版本下载地址:https://marlam.de/msmtp/download/ [root@--- ~]# wget https://marlam.de/msmtp ...

  8. restful_framework之视图组件

    一.基本视图 写一个出版社的增删查改resful接口 要自己事先创建好django项目,并创建好表,添加完记录 路由: url(r'^publish/$', views.PublishView.as_ ...

  9. Tomcat 下载与安装

    下载地址:http://tomcat.apache.org 根据自己电脑的系统下载Core节点下不同的版本.   Tomcat文件目录结构 bin:存放启动与关闭Tomcat的脚本文件 conf:存放 ...

  10. 资产管理系统 CMDB 讲解

    两年前笔者在一个中小型互联网公司做运维,当时我们经理在机房,花了半天找一台服务器,但是服务器搞错了,悲剧了^.^! 当时我们的做法是用了一个 Excel,很多时候更新不及时,重启一台机器.拔一根网线都 ...