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

    auto execution/self execution/ Immediate function http://www.jslint.com/ (function () { 'use strict' ...

  2. mybatis第二天_拓展——与spring整合以及逆向工程

    一.整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得sqlsession对象. 3.Mapper代 ...

  3. C# 获取北京时间 (根据纪元时间(1970/1/1)转换为DateTime)

    根据纪元时间(1970/1/1)转换为DateTime WebClient wc = new WebClient(); s= wc.DownloadString("http://api.ti ...

  4. BZOJ1068_压缩_KEY

    题目传送门 区间DP,设f[i][j][0/1]为i~j区间的压缩情况,1表示在插入了一个M. code: /********************************************* ...

  5. 09 ORM 多表操作,创建表,添加记录

    1.数据库表关系 1.一对多 为什么需要,重复字段太多 一对多关系表 Book id title price publish_id 1 python 100 1 2 php 200 2 3 go 10 ...

  6. 用C实现单隐层神经网络的训练和预测(手写BP算法)

    实验要求:•实现10以内的非负双精度浮点数加法,例如输入4.99和5.70,能够预测输出为10.69•使用Gprof测试代码热度 代码框架•随机初始化1000对数值在0~10之间的浮点数,保存在二维数 ...

  7. javaweb 解决jsp中${}传递中文值到后端以及get请求中文乱码的问题

    首先,不要用get传中文,我试了一些方法发现不行,所以果断决定用post传参, 这里用 encodeURI 进行一次编码传入后端 注意:${tplname} 要加 ' $.ajax({ url: '/ ...

  8. day01_概念

    1 网络分类: 1 按照范围: - 局域网:范围很小的网络,如一间办公室,一个公司 - 城域网:大致城市范围内的网络,半径几公里到几十公里 - 广域网:比城域网范围更大的 2 网络衡量标准 1 传输速 ...

  9. Siki_Unity_3-3_背包系统

    Unity 3-3 背包系统(基于UGUI) 任务1&2&3:演示.介绍.类图分析 背包面板.箱子面板.锻造合成面板.装备佩戴面板.商店面板等 面板的显示和隐藏.保存和加载.拾起物品. ...

  10. Jmeter实战

    Jmeter实战 入门篇 1.下载与使用 下载地址:http://jmeter.apache.org/download_jmeter.cgi 开源,基于java编写,所以得有jdk(jre)环境,下载 ...