浙大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 ...
随机推荐
- MongoDB学习之--增删改查(2)
昨天简单介绍了官方驱动操作MongoDB进行增删查操作的,今天继续介绍更新操作..... 方法简介 官方驱动中更新操作使用collection的Update方法,有泛型和非泛型两个版本: 其签名如下( ...
- 配置restful webservice 框架restkit
(配置restful webservice 框架restkit) RestKit 是一个开源的 objective-c 框架,容许在 iOS 和 Mac OS X 的 Objective-C 中与 R ...
- 安装Theano
参考文档 http://deeplearning.net/software/theano/install_centos6.html#install-centos6 安装依赖库 sudo yum ins ...
- python打包成window可执行程序
python程序可以通过python hello.py执行,但是需要安装python的解释器,并配置环境变量,打包成exe程序之后可以直接执行. 使用setup工具和py2exe可以做到这一点. 最简 ...
- LLVM小结
随笔- 5 文章- 0 评论- 10 LLVM小结 如果说gcc是FSF的传奇,llvm就是Chris Lattner的小清新.当然啦,想具体看看这位四处游山玩水还GPA 4.0的大神和他的 ...
- 总结C++中取成员函数地址的几种方法
这里, 我整理了4种C++中取成员函数地址的方法, 第1,2,4种整理于网上的方法, 第3种cdecl_cast是我自己想到的. 其中, 第4种(汇编)的方法不能在VC6上编译通过. 推荐使用第1,2 ...
- elasticsearch data importing
ElasticSearch stores each piece of data in a document. That's what I need. Using the bulk API. Trans ...
- sphinx2.8.8的配置文件
# # Sphinx configuration file sample # # WARNING! While this sample file mentions all available opti ...
- java.io.IOException: Unable to open sync connection!的解决方案
在学习Android的时候,经常是使用手机调试程序,很方便,后来 在使用手机调试程序的时候出现了 [2012-03-08 11:27:43 - Tea_marsListActivity] ------ ...
- 深刻理解HDFS工作机制
深入理解一个技术的工作机制是灵活运用和快速解决问题的根本方法,也是唯一途径.对于HDFS来说除了要明白它的应用场景和用法以及通用分布式架构之外更重要的是理解关键步骤的原理和实现细节.在看这篇博文之前需 ...