1020 Tree Traversals (25)(25 分)

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

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 postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. 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:

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

Sample Output:

4 1 6 3 5 7 2

已知后序中序求先序:
 void pre(int root, int begin, int end)
{
if (begin > end) return;
cout << post[root] << " ";
int p;
for (p = begin; p <= end; p++)
{
if (in[p] == post[root])
break;
}
pre(root - end + p - , begin, p - );
pre(root - , p + , end);
}

后序中最后一个元素为跟,找出跟在中序中的位置就可以确定左右子树的节点个数,然后就可以递归的去求解。

这个题是求层序遍历,开始使用的构造树然后再广度优先搜索的方法,比较繁琐。后来看柳婼的博客发现使用数组的方法构造树比较方便。

若当前根的下标为i,则左节点下标为 2 * i + 1,右节点下标为 2 * i + 2。

 #include <iostream>
#include <vector>
#include <math.h>
using namespace std; vector<int> post, in, level(, -);
void getLevel(int root, int begin, int end, int index); int main()
{
int N, i;
cin >> N;
post.resize(N);
in.resize(N);
for (i = ; i < N; i++)
cin >> post[i];
for (i = ; i < N; i++)
cin >> in[i];
getLevel(N - , , N - , );
int cnt = ;
for (int i : level)
{
if (i != -)
{
if (cnt > )
cout << " ";
cnt++;
cout << i;
if (cnt == N)
break;
}
}
return ;
} void getLevel(int root, int begin, int end, int index)
{
if (begin > end) return;
level[index] = post[root];
int p;
for (p = begin; p <= end; p++)
{
if (in[p] == post[root])
break;
}
getLevel(root - end + p - , begin, p - , * index + );
getLevel(root - , p + , end, * index + );
}
 

pat甲级1020中序后序求层序的更多相关文章

  1. SDUT OJ 数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...

  2. SDUT-2804_数据结构实验之二叉树八:(中序后序)求二叉树的深度

    数据结构实验之二叉树八:(中序后序)求二叉树的深度 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 已知一颗二叉树的中序 ...

  3. 算法进阶面试题03——构造数组的MaxTree、最大子矩阵的大小、2017京东环形烽火台问题、介绍Morris遍历并实现前序/中序/后序

    接着第二课的内容和带点第三课的内容. (回顾)准备一个栈,从大到小排列,具体参考上一课.... 构造数组的MaxTree [题目] 定义二叉树如下: public class Node{ public ...

  4. 给出 中序&后序 序列 建树;给出 先序&中序 序列 建树

    已知 中序&后序  建立二叉树: SDUT 1489 Description  已知一棵二叉树的中序遍历和后序遍历,求二叉树的先序遍历 Input  输入数据有多组,第一行是一个整数t (t& ...

  5. 二叉树 遍历 先序 中序 后序 深度 广度 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  6. 前序+中序->后序 中序+后序->前序

    前序+中序->后序 #include <bits/stdc++.h> using namespace std; struct node { char elem; node* l; n ...

  7. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

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

  8. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

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

  9. PAT (Advanced Level) 1124~1127:1124模拟 1125优先队列 1126欧拉通路 1127中序后序求Z字形层序遍历

    1124 Raffle for Weibo Followers(20 分) 题意:微博抽奖,有M个人,标号为1~M.从第S个人开始,每N个人可以获奖,但是已获奖的人不能重复获奖,需要跳过该人把机会留给 ...

随机推荐

  1. sqlserver2012——SqlCommand创建对象的三种方法

    1.使用不带参构造函数 SqlCommand cmd = new SqlCommand(); cmd.Connection = SqlConnnection对象: cmd.CommandText=Sq ...

  2. Unity 点乘与叉乘 计算敌人位置

    点乘:表示两个向量夹角 a*b=|a| * |b| * cos(ab),判断敌人在前后方 叉乘:表示两向量的法线

  3. vue入门(二)----模板与计算属性

    其实这部分内容我也是参考的官网:http://cn.vuejs.org/v2/guide/syntax.html,但是我还是想把自己不懂的知识记录一下,加深印象,也可以帮助自己以后查阅.所谓勤能补拙. ...

  4. 浅谈JavaScript -- 正则表达式

    什么是正则表达式? 正则表达式是由一个字符序列形成的搜索模式.可用于文本搜索和文本替换. 语法:/正则表达式主体/修饰符(可选) var patt=new RegExp(pattern,modifie ...

  5. eclipse中项目已经启动,可是tomcat一直显示在启动中

    一.异常描述 1. 在eclipse中启动tomcat,应用已经启动成功,但是tomcat仍然一直处于starting装填 二.分析原因 1. 更换8080端口为9080,启动tomcat,可以完整启 ...

  6. 为什么Python如此慢

    Python当前人气暴涨.它在DevOps,数据科学,Web开发和安全领域均有使用. 但是在速度方面没有赢得美誉. 这里有关于Python比较其他语言如,Java, C#, Go, JavaScrip ...

  7. C 语言实例 - 计算一个数的 n 次方

    C 语言实例 - 计算一个数的 n 次方 计算一个数的 n 次方,例如: ,其中 为基数, 为指数. 实例 - 使用 while #include <stdio.h> int main() ...

  8. ie-"此更新不适应于此电脑"

    cmd-dos命令 expand –F:* C:\update\Windows6.1-KB2533623-x64.msu C:\update\ dism.exe /online /Add-Packag ...

  9. EIGRP-7-可靠传输协议

    可靠传输协议(RTP,Reliable Transport Protocol)负责管理ElGRP数据包的发送和接收.可靠传输意味着传输是有保障的,并且数据包会被按顺序发送.这种传输效果是依靠Cisco ...

  10. JD孔_20160912

    1.买的 “航嘉(Huntkey)大白803 8位3米 总控开关 防过载保护 插座/排插/拖”  http://item.jd.com/1786149.html#product-detail 2.