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. python下一个转码的问题

    我想把一个quoted的字符串经过unquote处理后,打印出来.被unquote处理后的字串应该是utf-8的,因此还需要按照utf-8再做一次解码,代码如下:   import urllib im ...

  2. java解析xml实例——获取天气信息

    获取xml并解析其中的数据: package getweather.xml; import java.io.IOException; import java.util.HashMap; import ...

  3. SQL数据库操作整理

    1.规范 ①关键字与函数名称全部大写: ②数据库名称.表名称.字段名称全部小写: ③SQL语句必须以分号结尾. 2.数据库操作 // 1. 创建数据库,其中[]表示可以省略 CREATE { DATA ...

  4. WPF动画——故事板(Storyboard)

    1.XAML代码 <Window x:Class="故事板.MainWindow" x:Name="window" xmlns="http:// ...

  5. 未能加载文件或程序集“Oracle.DataAccess, Version=4.112.2.0, Culture=neutral, PublicKeyTok”

    1.首先看一下C:\Windows\assembly目录下是不是只有一个Oracle.DataAccess,我的版本是10,如果是只有一个,则往下看: 2.将完整的odp.net(目录下包含注册文件) ...

  6. 51nod1007(01背包)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1007 题意:中文题诶- 思路:尽量将一个数组分成两个相等的部 ...

  7. css3旋转立方体-_-

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  8. Object类、常用API

    Object类.常用API Object类.常用API Object类.常用API Object类.常用API Object类.常用API Object类.常用API

  9. Java基础笔记(七)—— 成员变量、静态变量、局部变量

    public class Test { int c; //成员变量(实例变量) static int s1; //静态变量(类变量)(全局变量) public static void main(Str ...

  10. ros indigo install

    http://wiki.ros.org/cn/indigo/Installation/Ubuntu