给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历

#include<bits/stdc++.h>

using namespace std;
const int N=;
int in[N];
int post[N]; typedef struct node;
typedef node *tree;
struct node
{
int data;
tree L;
tree R;
};
void print(tree &bt,int l1,int r1,int l2,int r2)
{
if(l1>r1||l2>r2) return;
int mid=l2;
while(in[mid]!=post[r1]) mid++;
bt=new node;
bt->data=post[r1];
bt->L=NULL;
bt->R=NULL;
print(bt->L,l1,l1+mid-l2-,l2,mid-);
print(bt->R,l1+mid-l2,r1-,mid+,r2);
}
vector<int>p;
void s(tree bt)
{
queue<tree>Q;
Q.push(bt);
while(!Q.empty()){
tree u=Q.front();
Q.pop();
p.push_back(u->data);
if(u->L) Q.push(u->L);
if(u->R) Q.push(u->R);
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%d",&post[i]);
for(int i=;i<n;i++) scanf("%d",&in[i]);
tree bt;
print(bt,,n-,,n-);
s(bt);
for(int i=;i<p.size();i++){
if(i) printf(" ");
printf("%d",p[i]);
}
printf("\n");
return ;
}

1020 Tree Traversals (25 分)(二叉树的遍历)的更多相关文章

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

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

  2. PAT Advanced 1020 Tree Traversals (25 分)

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

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

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

  4. 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)

    题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...

  5. 1020 Tree Traversals (25分)思路分析 + 满分代码

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  6. 1020 Tree Traversals (25 分)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

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

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

  8. PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]

    题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...

  9. 1020. Tree Traversals (25) ——树的遍历

    //题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印 PS:以前对于这种用指针的题目是比较头痛的,现在做了一些链表操作后,感觉也不难 先通过后续中序建一棵树,然后通过BFS遍历这棵树 ...

  10. 1020. Tree Traversals (25) -BFS

    题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...

随机推荐

  1. 探索性数据分析EDA综述

    目录 1. 数据探索的步骤和准备 2. 缺失值处理 为什么需要处理缺失值 Why data has missing values? 缺失值处理的技术 3. 异常值检测和处理 What is an ou ...

  2. 前端面试题二(来自前端网http://www.qdfuns.com/notes/23515/fa8b1e788ac39b04108fc33e5b543c4a.html)

    HTML&CSS 1.请描述一下 cookies,sessionStorage 和 localStorage 的区别? cookie是网站为了标示用户身份而储存在用户本地终端(Client S ...

  3. 深入理解HashMap底层实现原理

    一.HashMap (JDK8)put(K key, V value)底层实现 1. 首先判断这个hashmap是否为空,为空就初始化一个hashmap 2. 根据key 计算hashcode()值, ...

  4. java基础IO流 复制键盘录入的目录,复制其中的.java文件到指定目录,指定目录中有重名,则改名 对加密文件计算字母个数

    package com.swift.jinji; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; im ...

  5. tree树形

    /**  * tree  * @param menuBeans  * @param pid  * @return  */ public JSON  makeTree(List<MenuBean& ...

  6. JavaScript的执行机制

    JavaScript是单线程的,任务的执行时自上而下的,这就有了一个问题,当遇到一个比较耗时的任务时,下面的代码就会被阻塞,这就意味着卡死.所以js是有异步的,它的实现主要是通过事件循环(event ...

  7. 基于Vue的SPA如何优化页面加载速度

    常见的几种SPA优化方式 减小入口文件体积 静态资源本地缓存 开启GZip压缩 使用SSR ..... 减小入口文件体积,常用的手段是路由懒加载,开启路由懒加载之后,待请求的页面会单独打包js文件,使 ...

  8. Codeforces#498F. Xor-Paths(折半搜索)

    time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. lintcode_111_爬楼梯

    爬楼梯   描述 笔记 数据 评测 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的 ...

  10. vue笔记v-bind

    把message的信息绑定给title,运行出来的结果是<span title="you loaded this page on +Mon Nov 21 2016 18:28:38 G ...