浙大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 ...
随机推荐
- TFS二次开发、C#知识点、SQL知识
TFS二次开发.C#知识点.SQL知识总结目录 TFS二次开发系列 TFS二次开发系列:一.TFS体系结构和概念 TFS二次开发系列:二.TFS的安装 TFS二次开发系列:三.TFS二次开发的第一 ...
- Erasure Coding in WAS简单译文
原文:<Erasure Coding in Windows Azure Storage.pdf>,地址:https://www.usenix.org/system/files/confer ...
- 在WebStorm中集成Karma+jasmine进行前端单元测试
在WebStorm中集成Karma+jasmine进行前端单元测试 前言 好久没有写博了,主要还是太懒=.=,有点时间都去带娃.看书了,今天给大家分享一个原创的小东西,如果大家对TDD或者BDD有兴趣 ...
- MongoDB深圳用户组线下活动召集
MongoDB线下用户组是由全世界MongoDB爱好者发起的不定期线下交流活动.目前全球有100多个MongoDB用户组,3万5千多爱好者参与.用户组活动的形式通常会有一到两个MongoDB相关的技术 ...
- sftp 服务器外网访问设置
这个是需要在本机上设定用户名的.根据不同的用户权限.然后让不同的人来到本机上取得数据. 参考一下有用的文章:http://www.ericstockwell.com/?p=54 (强烈建议 ...
- grub 的安装与使用
安装与使用grub 要开始探究 GRUB 的精妙之处,首先需要下载.编译和安装它.但不要害怕 -- 根本不会修改您的引导记录 -- 我们只是要编译和安装 GRUB,就像其它程序一样,在此过程中我们可以 ...
- 最小包围多边形(凸包;最小包围点集)——C代码例子
本文来自:http://alienryderflex.com/smallest_enclosing_polygon/ 这个C代码例子需要一群2维点集,如下图所示: 要获得包含这些点的最小多边形如下图所 ...
- All about Performing User-Managed Database Recovery
Automatic Recovery with SET AUTORECOVERY ======================================== Issuing SET AUTORE ...
- 使用 EasyUI 创建左侧导航菜单
使用 JQuery EasyUI 创建左侧导航菜单,菜单的数据由后台服务提供. 效果图 HTML 元素 <div id="menuAccordion"></div ...
- Dubbo源码学习--服务是如何发布的
相关文章: Dubbo源码学习--服务是如何发布的 Dubbo源码学习--服务是如何引用的 ServiceBean ServiceBean 实现ApplicationListener接口监听Conte ...