POJ 2255 Tree Recovery [二叉树]
来源:互联网 发布:sinead o connor 知乎 编辑:程序博客网 时间:2024/05/29 15:10
题意:
给前序和中序,求后序便利。
思路:
惭愧,把最原始的都忘了,记得这应该是数据结构的例题之类的知识。
所以特地把最原始的树也构造一遍,再后序输出,练练手。
#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>#include<queue>#include<cmath>#define Max(a,b) ((a)>(b)?(a):(b))#define Min(a,b) ((a)<(b)?(a):(b))#define Abs(a) ((a)>0?(a):(-(a)))#define llong long long intusing namespace std;const int N=30;const int inf=(1<<30);int n,m;char str1[N],str2[N];struct Node{int father;int leftson;int rightson;}node[N];void preorder(char * s1,char * s2,int len,int pre,int son)//rebuild the tree{int now=s1[0]-'A'+1;if(len==1){if(son)node[pre].rightson=now;else node[pre].leftson=now;node[now].father=pre;return;}int pos;for(pos=0;pos<len;pos++){if(s2[pos]==s1[0]){break;}}if(pos){preorder(s1+1,s2,pos,now,0);}if(len-pos-1){preorder(s1+pos+1,s2+pos+1,len-pos-1,now,1);}if(son) node[pre].rightson=now;else node[pre].leftson=now;node[now].father=pre;}void print_pod(int now){if(!now) return;print_pod(node[now].leftson);print_pod(node[now].rightson);printf("%c",now+'A'-1);}int main(){while(scanf(" %s %s",str1,str2)!=EOF){memset(node,0,sizeof(node));n=strlen(str1);preorder(str1,str2,n,0,0);print_pod(str1[0]-'A'+1);printf("\n");}return 0;}
- POJ 2255 Tree Recovery 二叉树遍历
- POJ 2255 Tree Recovery [二叉树]
- Poj 2255-Tree Recovery//二叉树,递归
- POJ 2255 Tree Recovery ( 二叉树)
- POJ 2255 Tree Recovery 二叉树基础
- POJ 2255 Tree Recovery 二叉树恢复
- POJ 2255 Tree Recovery(二叉树)
- POJ 2255 Tree Recovery(二叉树重建)
- POJ 2255 Tree Recovery (二叉树遍历)
- poj 2255 Tree Recovery(二叉树)
- 【POJ 2255 Tree Recovery】+ 二叉树
- POJ 2255 Tree Recovery 二叉树
- POJ 2255 Tree Recovery 二叉树的遍历
- POJ 2255 Tree Recovery 二叉树+遍历+递归
- poj 2255 Tree Recovery(求后序遍历,二叉树)
- poj 2255 Tree Recovery(二叉树的遍历)
- POJ 题目2255Tree Recovery(二叉树)
- POJ 2255 Tree Recovery(二叉树遍历)
- 编写通用的DAO类
- 电子商务网站 Android 客户端
- VIM 资源收集
- Ubuntu Server crontab 自动备份
- java nio socket
- POJ 2255 Tree Recovery [二叉树]
- Vertex Baking
- Android 源代码在线查看
- 如何制作一个总是位于屏幕底部的按钮
- 对话框 (AlertDialog & ProgressDialog)
- CCNP_route笔记7 从EIGRP的MTU设置看hold-time
- android SharedPreferences(供选项使用)
- 如何将Android ImageView放到布局的中心
- AbstractApplicationContext -> destroy()