浙大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 ...
随机推荐
- MSSQL数据库迁移到Oracle
MSSQL数据库迁移到Oracle 最近要把一个MSSQL数据库迁移到Oracle上面,打算借助PowerDesigner这个软件来实现;今天简单研究一下这个软件的运用;把一步简单的操作步骤记录下来: ...
- (翻译) Android Accounts Api使用指南
本文翻译自Udinic的文章Write your own Android Authenticator,可能需要FQ才能阅读.这是译者目前能找到的介绍如何使用Android的Accounts Api最好 ...
- iOS 应用测试最佳实践
IOS改变了移动游戏,毫无疑问.它为“移动时代”铺平了道路,通过简单的用户体验提供惊人的功能.然而涉及到测试与监控,使用iPhone/iPad移动应用程序是除了简单之外的任何事情. 随着IOS应用市场 ...
- Android 手机进入不了fastboot模式的解决方案
本方案仅针对linux terminal下刷手机img文件的情况: fastboot的通常流程如下: adb reboot bootloader //进入bootloader 模式 fastb ...
- flowplayer视频播放插件
flowplayer视频播放插件 最近项目中需要添加播放视频的功能,视频文件是flv格式的.在网上找了一些jQuery视频播放插件,还是觉得“flowplayer”要好一些.特将使用方法记录一下. f ...
- U盘安装CentOS 6.4 + Windows 7双系统 (Windows 7下安装 CentOS 6.4)
最近在看<鸟哥私房菜:基础学习篇>,觉得很不错,想要装个windows 7 和 CentOS 6.4 双系统,在网上找了很多教程,觉得乱七八糟的,弄得很复杂,而且很多都不是很完整,对于新手 ...
- C#开发学习——SqlHelper的应用
使用App.config配置文件封装连接字符串,方便重复使用--->添加App.conifg配置文件--->Add : ConnectionString:--->添加引用 <? ...
- __import__简介
__import__() import 语句通过调用__import__()来完成工作,提供这个函数是为了让有特殊需要的用户覆盖他,实现自定义.__import__最大的好处就是可以使程序在r ...
- 【原】手写一个promise
上一篇文章中,我们介绍了Promise的基本使用,在这篇文章中,我们试着自己来写一个Promise,主要是学习Promise的内部机制,学习它的编程思想. !!!备注:本文写的不好,仅供自己学习之用, ...
- Kali命令集
转载自:http://www.shiyanbar.com/questions/980 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显 ...