problem

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

tip

给出后序与中序遍历,要求层次遍历。

answer

#include<iostream>
#include<queue>
#define Max 33 using namespace std; int n, back[Max], mid[Max];
//int tree[Max];
struct Node{
int d;
Node *l;
Node *r;
};
Node *root; void Print(){
queue<Node*> q;
q.push(root);
while(!q.empty()){
Node *t = q.front(); q.pop();
if(!t->d) continue;
if(!t->l && !t->r && q.empty()) cout<<t->d;
else cout<<t->d<<" "; if(t->l) q.push(t->l);
if(t->r) q.push(t->r);
}
// cout<<endl;
} Node* DFS(int left, int right, int midLeft, int midRight){
if(left > right) return NULL; Node *a = new Node();
int num = back[right];
int numIndex = -1;
for(int i = midLeft; i <= midRight; i++){
if(mid[i] == num) numIndex = i;
}
int lNum = numIndex - midLeft, rNum = midRight - numIndex;
a->d = num;
Print();
a->l = DFS(left, left + lNum -1, numIndex - lNum, numIndex-1);
a->r = DFS(right-rNum, right-1, numIndex+1, numIndex+rNum);
return a;
} int main(){
// freopen("test.txt", "r", stdin);
ios::sync_with_stdio(false); cin>>n;
for(int i = 0; i < n; i++){
cin>>back[i];
}
for(int i = 0; i < n; i++){
cin>>mid[i];
}
root = new Node();
root = DFS(0, n-1, 0, n-1);
Print();
return 0;
}

exprience

  • 二叉树要多写几次,就熟悉了。

1020 Tree Traversals (25)(25 point(s))的更多相关文章

  1. 03-树3 Tree Traversals Again(25 point(s)) 【Tree】

    03-树3 Tree Traversals Again(25 point(s)) An inorder binary tree traversal can be implemented in a no ...

  2. 03-树3 Tree Traversals Again(25 分)

    题目 链接 分析 push是二叉树前序遍历的结果,pop是二叉树中序遍历的结果,所以这个题就是已知前序遍历和中序遍历,求后序遍历. AC代码 #include "bits/stdc++.h& ...

  3. 03-树3 Tree Traversals Again (25 分)

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...

  4. 03-树3 Tree Traversals Again (25 分)

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...

  5. 1086 Tree Traversals Again (25 分)(二叉树的遍历)

    用栈来模拟一棵二叉树的先序遍历和中序遍历过程,求这棵二叉树的后序遍历 由题棵知道:push是先序遍历 pop是中序遍历 #include<bits/stdc++.h> using name ...

  6. 【PAT】1020 Tree Traversals (25)(25 分)

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

  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 1020 Tree Traversals (25 分)

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

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

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

随机推荐

  1. 【CodeForces】700 D. Huffman Coding on Segment 哈夫曼树+莫队+分块

    [题目]D. Huffman Coding on Segment [题意]给定n个数字,m次询问区间[l,r]的数字的哈夫曼编码总长.1<=n,m,ai<=10^5. [算法]哈夫曼树+莫 ...

  2. JS比较两个数字大小

    js不能直降比较两·个数大小,要先转化为整数再比较大小. parseInt()转化. 出处:http://www.jb51.net/article/98251.htm

  3. Oracle分析函数Over()

    一.Over()分析函数 说明:聚合函数(如sum().max()等)可以计算基于组的某种聚合值,但是聚合函数对于某个组只能返回一行记录.若想对于某组返回多行记录,则需要使用分析函数. 1.rank( ...

  4. C#反射-Assembly.Load、LoadFrom与LoadFile

    反射Demo: public class Person { public int Age; public void SayHello() { Console.WriteLine("Hello ...

  5. 【译】第八篇 Replication:合并复制-How it works

    本篇文章是SQL Server Replication系列的第八篇,详细内容请参考原文. 在这一系列的前几篇你已经学习了如何在多服务器环境中配置合并复制.这一篇将介绍合并代理并解释它在复制过程中扮演的 ...

  6. 【译】第四篇 Replication:事务复制-订阅服务器

    本篇文章是SQL Server Replication系列的第四篇,详细内容请参考原文. 订阅服务器就是复制发布项目的所有变更将传送到的服务器.每一个发布需要至少一个订阅,但是一个发布可以有多个订阅. ...

  7. 差分约束系统+(矩阵)思维(H - THE MATRIX PROBLEM HDU - 3666 )

    题目链接:https://cn.vjudge.net/contest/276233#problem/H 题目大意:对于给定的矩阵  每一行除以ai  每一列除以bi 之后 数组的所有元素都还在那个L- ...

  8. spfa+floyed+最长路+差分约束系统(F - XYZZY POJ - 1932)(题目起这么长感觉有点慌--)

    题目链接:https://cn.vjudge.net/contest/276233#problem/F 题目大意:给你n个房子能到达的地方,然后每进入一个房子,会消耗一定的生命值(有可能是负),问你一 ...

  9. UNIX环境高级编程 第2章 UNIX标准及实现

    在过去的将近25年时间,人们为了UNIX的标准化做出了种种努力,这使得程序在不同版本的UNIX系统之间的移植相当容易. ISO C 1989年,C语言首个标准得到批准,其为C89.次年,一个带有小改动 ...

  10. JavaScript中函数参数的值传递和引用传递

    结论: 对于数字.字符串等基本类型变量,是将它们的值传递给了函数参数,函数参数的改变不会影响函数外部的变量. 对于数组和对象等是将对象(数组)的变量的值传递给了函数参数,这个变量保存的指向对象(数组) ...