1020 Tree Traversals (25 分)(二叉树的遍历)
给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历
#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 分)(二叉树的遍历)的更多相关文章
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT Advanced 1020 Tree Traversals (25 分)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- 【PAT甲级】1020 Tree Traversals (25 分)(树知二求一)
题意: 输入一个正整数N(N<=30),给出一棵二叉树的后序遍历和中序遍历,输出它的层次遍历. trick: 当30个点构成一条单链时,如代码开头处的数据,大约1e9左右的结点编号大小,故采用结 ...
- 1020 Tree Traversals (25分)思路分析 + 满分代码
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 【PAT】1020 Tree Traversals (25)(25 分)
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT Advanced 1020 Tree Traversals (25) [⼆叉树的遍历,后序中序转层序]
题目 Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder an ...
- 1020. Tree Traversals (25) ——树的遍历
//题目 通过后续遍历 中序遍历 得出一棵树 ,然后按树的层次遍历打印 PS:以前对于这种用指针的题目是比较头痛的,现在做了一些链表操作后,感觉也不难 先通过后续中序建一棵树,然后通过BFS遍历这棵树 ...
- 1020. Tree Traversals (25) -BFS
题目如下: Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder ...
随机推荐
- 2017.9.28 web设计简单的购物车应用案例--session的简单应用
该购物过程是在session范围内完成的,需要使用session对象实现信息的共享 (1)购买“肉类”商品的页面 <%@ page language="java" impor ...
- kmeans聚类中的坑 基于R shiny 可交互的展示
龙君蛋君 2015年5月24日 1.背景介绍 最近公司在用R 建模,老板要求用shiny 展示结果,建模的过程中用到诸如kmean聚类,时间序列分析等方法.由于之前看过一篇讨论kmenas聚类针对某一 ...
- javaweb基础(36)_jdbc进行批处理
在实际的项目开发中,有时候需要向数据库发送一批SQL语句执行,这时应避免向数据库一条条的发送执行,而应采用JDBC的批处理机制,以提升执行效率. JDBC实现批处理有两种方式:statement和pr ...
- vs2012或vs2013调试卡 关闭调试卡
以前vs2013就有这个问题.没有解决.今天又装了vs2012.又遇到了.特别郁闷. 今天一定要解决.网上百度了.很久.可能关键字有问题.没有找到好的办法. 找到的办法有.显卡问题.不是管理员运行问题 ...
- An error occurred at line: 1 in the generated java file问题处理
tomcat6启动后,加载jsp页面报错,提示无法将jsp编译为class文件,主要报错信息如下: An error occurred at line: 1 in the generated java ...
- caffe中的sgd,与激活函数(activation function)
caffe中activation function的形式,直接决定了其训练速度以及SGD的求解. 在caffe中,不同的activation function对应的sgd的方式是不同的,因此,在配置文 ...
- 破解weblogic(数据库)密码
破解weblogic(数据库)密码所需步骤 注意:本例子本人以本地weblogic为列,必须已经安装weblogic 1.需要问题件 1>.数据源配置文件HKS***-****-jdbc.xml ...
- POJ2154 Color(Polya定理)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11654 Accepted: 3756 Description Bead ...
- Win10上Anaconda环境下python3.6安装和使用pyinstaller
一.安装步骤 1. 电脑是win10,安装的Python3.6 2. 在Scripts文件夹下执行pip install pyinstaller, 安装成功后下载pyinstaller安装包,解压之后 ...
- Python面向对象--高级(一)
## 属性的类型 - 属性可分为类属性和实例属性 - 实例属性可以通过在类中使用self定义,或者直接在类外部使用实例变量定义 class Person(object): def __init__(s ...