浙大pat1020题解
1020. Tree Traversals (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
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;
#define MaxSize 32
struct Node
{
int data;
Node *left;
Node *right;
};
queue<Node*>result;
vector<int> vec;
Node *CreatNode(int *post,int*in,int len)
{
if(len == NULL)
return NULL;
int i = len-1;
while(in[i]!=post[len-1]) i--;
Node *p;
p =new Node;
p->data = in[i];
p->left = CreatNode(post,in,i);
p->right = CreatNode(post+i,in+i+1,len-i-1);
return p;
}
void LevelPrint(Node*p)
{
if(p!=NULL) result.push(p);
while(!result.empty())
{
Node * temp = result.front();
if(temp->left!=NULL) result.push(temp->left);
if(temp->right!=NULL) result.push(temp->right);
vec.push_back(temp->data);
result.pop();
}
}
int main()
{
int Post[MaxSize],In[MaxSize];
int len;
cin >> len;
for(int i=0;i<len;i++)
cin >> Post[i];
for(int j=0;j<len;j++)
cin >> In[j];
Node*root = CreatNode(Post,In,len);
LevelPrint(root);
int i;
for(i=0;i<vec.size()-1;i++)
cout<<vec[i]<<" ";
cout<<vec[i]<<endl;
return 0;
}
浙大pat1020题解的更多相关文章
- 浙大pat1050题解
1050. String Subtraction (20) 时间限制 10 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard Given two string ...
- 浙大pat1013题解
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- 浙大pat1009题解
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 浙大pat1042题解
1042. Shuffling Machine (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shu ...
- 浙大pat1019题解
1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- 浙大pat 1011题解
With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...
- 浙大PAT 7-06 题解
#include <stdio.h> #include <iostream> #include <algorithm> #include <math.h> ...
- 浙大pat 1012题解
1012. The Best Rank (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue To eval ...
- 浙大 pat 1003 题解
1003. Emergency (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
随机推荐
- 分析Sizzle引擎 - 词法解析
分析Sizzle引擎 - 词法解析 声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢! 浏览器从下载文档到显示页面的过程是个复杂的过程,这里包含了重绘和重排.各家浏览器引擎的工 ...
- MongoDB服务安装
0.解压压缩包:D:\lab\mongodb 1.创建目录: D:\lab\mongodb\data D:\lab\mongodb\data\db D:\lab\mongodb\data\log D: ...
- JavaScript怎么上传图片
JavaScript怎么上传图片 在XMLHttpRequest Level2出台之前,大多数的异步上传图片都是利用iframe去实现的. 至于具体的实现细节,我就不在这边啰嗦的,Google一下就有 ...
- 绘制基本图形和线型(StrokeStyle)的设置详解
绘制基本图形和线型(StrokeStyle)的设置详解 目前,在博客园上,相对写得比较好的两个关于Direct2D的教程系列,分别是万一的Direct2D系列和zdd的Direct2D系列.有兴趣的网 ...
- MVVM与Knockout
MVVM与Knockout 前言 今天搞的有点快,因为上午简单研究了下MVC,发现MVC不太适合前端开发,然后之前看几位前端前辈都推荐前端使用MVVM,但是我对其还不甚了解,所以我觉得下午还是应该先看 ...
- linux下编译运行驱动
linux下编译运行驱动 嵌入式linux下设备驱动的运行和linux x86 pc下运行设备驱动是类似的,由于手头没有嵌入式linux设备,先在vmware上的linux上学习驱动开发. 按照如下方 ...
- 使用JavaScript重定向URL参数
本人从网上查找(如有雷同,不胜荣幸.),并进行了修改,简单粗暴,实现使用JavaScript重置url参数 1.字符拼接形式 function setUri(para, val) { var strN ...
- Epicor系统二次开发
Epicor系统二次开发 一.获取或修改界面EpiDataView的字段数据(Get EpiDataView data) C# EpiDataView edv = (EpiDataView)oTran ...
- 创建基本的2D场景(part2)
让我们继续来学习Unity2D游戏场景的制作,本文分为以下3个部分: · 添加角色和控制 . 添加2D物理阻挡 · 添加2D效果 通过制作一个移动帽子接保龄球的小游戏,我们可以学习到任何创建游戏对象, ...
- [ios-必看] iOS 下实现解压缩
http://blog.csdn.net/lyy_whg/article/details/11971581 http://blog.sina.com.cn/s/blog_833996210100udk ...