二叉树重构
来源:互联网 发布:yii商城源码 编辑:程序博客网 时间:2024/06/07 14:11
给你一颗真二叉树(节点要么没有孩子,要么有两个孩子)的前序和后序遍历输出中序遍历序列。
/************************************************************************* > File Name: Euler.cpp > Author: acvcla > QQ: > Mail: acvcla@gmail.com > Created Time: 2014年10月30日 星期四 11时19分11秒 ************************************************************************/#include<iostream>#include<cstring>#include<cstdio>using namespace std;const int maxn=4000000+5;#define rep(i,a,b) for(int i=(a);i<=(b);i++)struct tree{int date;tree *l,*r;tree(int val=0):date(val){l=r=0;}}M[maxn];int cnt;tree *newnode(int val){M[cnt].l=M[cnt].r=0;M[cnt].date=val;cnt++;return &M[cnt-1];}int A[maxn],B[maxn],ans[maxn],cot,n,pos1[maxn],pos2[maxn];tree* built(int l,int r,int cur){tree *now=NULL;now=newnode(B[r]);if(l==r)return now;++cur,--r;now->l=built(l,pos1[A[cur]],cur);now->r=built(pos1[A[cur]]+1,r,pos2[B[r]]);return now;}struct V{void operator () (int x){ans[++cot]=x;}};template<typename T>void dfs(tree *p,T &vist){if(!p)return;dfs(p->l,vist);vist(p->date);dfs(p->r,vist);}char ch;void read(int &x){x=ch=0;while(ch<'0'||ch>'9')ch=getchar();while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}}int main(){while(~scanf("%d",&n)){rep(i,1,n){read(A[i]);pos2[A[i]]=i;}rep(i,1,n){read(B[i]);pos1[B[i]]=i;}cnt=cot=0;tree *root=built(1,n,1);V cacu;dfs(root,cacu);rep(i,1,cot)printf("%d%c",ans[i],i==cot?'\n':' ');}return 0;}
3 0
- 二叉树重构
- 二叉树重构
- 二叉树重构
- 二叉树重构
- poj2255Tree Recovery【二叉树重构】
- Java实现二叉树重构
- 剑指Offer之二叉树重构
- 关于二叉树重构的思索
- 剑指offer 二叉树重构问题
- 真二叉树重构(Proper Rebuild)
- Tsinghua OJ:真二叉树重构(Proper Rebuild)
- 二叉树、二叉堆
- THU数据结构编程作业一:真二叉树重构(Proper Rebuild)
- PAT L2-006. 树的遍历(二叉树重构)
- 二叉树
- 二叉树
- 二叉树
- 二叉树
- jquery标题栏伸缩以及标题栏背景图跟着切换
- Android权限大全
- Spring 中数据源和数据库连接池配置的几种方法
- 递归函数的执行机制和运用
- 再看知名应用背后的第三方开源项目
- 二叉树重构
- net.bridge.bridge-nf-call-iptables=1
- GDI绘制简单的图形
- BingMap地图如何显示中文
- 安装ZooKeeper
- TASKLIST
- php递归函数实现无限级树型菜单
- C#中DllImport用法汇总
- Serial Port Profile (SPP)