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. 【leetcode 简单】 第九十五题 数字转换为十六进制数

    给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法. 注意: 十六进制中所有字母(a-f)都必须是小写. 十六进制字符串中不能包含多余的前导零.如果要转化的数 ...

  2. 21、List遍历时修改元素的问题

    List迭代时修改元素的问题 请编写代码完成以下需求:判断一个List里面是否包含monkey,如果包含的话,向集合中添加1024这个字符串.‘ package com.monkey1024.list ...

  3. 工具_HBuilder工具使用技巧

    https://www.cnblogs.com/xiaohouzai/p/7696152.html

  4. php实现异步请求

    PHP开启异步多线程执行脚本  装载自:http://www.cnblogs.com/clphp/p/4913214.html 场景要求 客户端调用服务器a.php接口,需要执行一个长达5s-20s不 ...

  5. PL/SQ连接oracle,L 新建表的时候, virtual那一列是什么意思

    Virtual标示该栏位是否为虚拟列. https://www.2cto.com/database/201306/216917.html

  6. SolrJ API 官方文档最佳实践

    以下内容译自Solr Wiki官方文档,版权没有,随意转载. Solrj 是一个访问solr的Java客户端.它提供了一个java接口用于添加更新和查询solr索引.本页面介绍SolrJ最新版本1.4 ...

  7. Nginx实现代理和用户验证

    1.下载Nginx 首先去官网http://nginx.org/en/download.html下载需要的版本即可,无需安装,只需要打开nginx.exe文件,nginx.exe的服务就开启了.打开h ...

  8. day07作业

    import java.util.Scanner; class SsqGame { public static void main(String[] args) { int total = 0;//买 ...

  9. Oracle 中count(1) 、count(*) 和count(列名) 函数的区别

    1)count(1)与count(*)比较: 1.如果你的数据表没有主键,那么count(1)比count(*)快2.如果有主键的话,那主键(联合主键)作为count的条件也比count(*)要快3. ...

  10. jQuery对象与JS原生对象之间的转换

    1.将jQuery转换为dom对象的方法 [index] 或者.get(index): a.$(“#form”)[index] ,该方法获取form元素的dom对象 b.$(“#form”).get( ...