http://acm.hdu.edu.cn/showproblem.php?pid=1710
2012-04-01 15:19
393 查看
二叉树遍历。。。。。。。。。。。。。。飘过
#include<iostream>
#include<string.h>
#define N 1005
using namespace std;
int pre
,mid
;
void build(int s,int s1,int n,bool flag)
{
if(n<=0) return;
int i;
for(i=0;i<n&&pre[s]!=mid[s1+i];++i);
build(s+1,s1,i,false);
build(s+1+i,s1+i+1,n-1-i,false);
if(flag) cout<<pre[s]<<endl;
else cout<<pre[s]<<" ";
}
int main()
{
int n;
while(cin>>n)
{
for(int i=0;i<n;++i)
cin>>pre[i];
for(int i=0;i<n;++i)
cin>>mid[i];
build(0,0,n,true);
}return 0;
}
#include<iostream>
#include<string.h>
#define N 1005
using namespace std;
int pre
,mid
;
void build(int s,int s1,int n,bool flag)
{
if(n<=0) return;
int i;
for(i=0;i<n&&pre[s]!=mid[s1+i];++i);
build(s+1,s1,i,false);
build(s+1+i,s1+i+1,n-1-i,false);
if(flag) cout<<pre[s]<<endl;
else cout<<pre[s]<<" ";
}
int main()
{
int n;
while(cin>>n)
{
for(int i=0;i<n;++i)
cin>>pre[i];
for(int i=0;i<n;++i)
cin>>mid[i];
build(0,0,n,true);
}return 0;
}
相关文章推荐
- http://acm.hdu.edu.cn/showproblem.php?pid=1905 素数判断 + 二分
- http://acm.hdu.edu.cn/showproblem.php?pid=2689
- 01背包:http://acm.hdu.edu.cn/showproblem.php?pid=2955
- http://acm.hdu.edu.cn/showproblem.php?pid=2795 更新节点构造线段数很关键,询问特殊
- Holedox Eating &&http://acm.hdu.edu.cn/showproblem.php?pid=4302
- http://acm.hdu.edu.cn/showproblem.php?pid=2604
- http://acm.hdu.edu.cn/showproblem.php?pid=1540 更新节点,询问节点所在的位置有多少连续的区间
- http://acm.hdu.edu.cn/showproblem.php?pid=1010
- 栈的简单应用 HDU 1022 http://acm.hdu.edu.cn/showproblem.php?pid=1022
- Advanced Fruits(链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503)
- 找新朋友(http://acm.hdu.edu.cn/showproblem.php?pid=1286)
- http://acm.hdu.edu.cn/showproblem.php?pid=1039..借鉴了别人的一些方法。。
- 来个图论,http://acm.hdu.edu.cn/showproblem.php?pid=2112
- (博客搬迁啦~)Max Sum Plus Plus(http://acm.hdu.edu.cn/showproblem.php?pid=1024)
- http://acm.hdu.edu.cn/showproblem.php?pid=3501 欧拉函数
- http://acm.hdu.edu.cn/showproblem.php?pid=4614
- http://acm.hdu.edu.cn/showproblem.php?pid=1166
- 最大报销额 http://acm.hdu.edu.cn/showproblem.php?pid=1864
- http://acm.hdu.edu.cn/showproblem.php?pid=1698 成段更新,总区间求和
- http://acm.hdu.edu.cn/showproblem.php?pid=1055&&Color a Tree