二叉树的还原(先中 + 后中)
来源:互联网 发布:java接口文档模板 编辑:程序博客网 时间:2024/06/05 23:57
#include <iostream>#include <sstream>#include <cstring>#include <cstdio>#include <cctype>#include <cmath>#include <map>#include <set>#include <list>#include <queue>#include <deque>#include <stack>#include <vector>#include <algorithm>#define inf 0x3f3f3f3f#define PI 3.14159265358979323#define SCD(a) scanf("%d",&a)#define SCDD(a,b) scanf("%d%d",&a,&b)#define SCF(a) scanf("%lf",&a)#define PTD(a) printf("%d\n",a)#define PTS(a) printf("%s\n",a)#define MST(a) memset(a, 0, sizeof(a))using namespace std;// PTA 还原二叉树const int L = 10001;int n, m;struct BiTNode{ char data; BiTNode *lchild, *rchild;};BiTNode *T;BiTNode* pre_in(const char* pre,const char* in, int n){ int i; if(n==0) return NULL; BiTNode* head = new BiTNode; head->data = pre[0]; for(i=0;i<n;i++){ if(pre[0] == in[i]) break; } BiTNode* lchild = pre_in(pre+1,in,i); BiTNode* rchild = pre_in(pre+i+1,in+i+1,n-i-1); head->lchild = lchild; head->rchild = rchild; return head;}BiTNode* post_in(const char* post,const char* in, int n){ if(n==0) return NULL; BiTNode* head = new BiTNode; head->data = post[n-1]; int i; for(i=0;i<n;++i){ if( post[n-1] == in[i]) break; } BiTNode* lchild = post_in(post,in,i); BiTNode* rchild = post_in(post+i,in+i+1,n-i-1); head->lchild = lchild; head->rchild = rchild; return head;}int Depth(BiTNode* &T){ if(T==NULL) return 0; else return max(Depth(T->lchild),Depth(T->rchild)) + 1;}int main(){ int i ,j ,a ,b ,t; SCD(n); char pre[L],in[L],pos[L]; /* //二选一 for(i=0;i<n;i++) cin>>pre[i]; for(i=0;i<n;i++) cin>>pos[i]; */ for(i=0;i<n;i++) cin>>in[i]; /* //对应二选一 T = post_in(pos,in,n); T = pre_in(pre,in,n); */ cout<<Depth(T)<<endl; return 0;}/*先中:9ABDFGHIECFDHGIBEAC5后中:9FHIGDEBCAFDHGIBEAC5*/
阅读全文
1 0
- 二叉树的还原(先中 + 后中)
- 二叉树的还原(先序+中序)
- 已知二叉树的先序遍历(preorder)和中序遍历(inorder) 或 中序和后序(postorder),还原该二叉树
- 根据二叉树的先序序列和中序序列还原二叉树并打印后序序列
- 二叉树的递归实现(先,中,后)
- 牛客网的题,重建二叉树。(用树的先序,中序,还原二叉树)
- 二叉树遍历(中序,先序->后序;中序,后序->先序)
- 二叉树的应用-先序遍历中序遍历还原二叉树
- 根据二叉树的先序和中序遍历还原二叉树
- 二叉树的先序遍历、中序遍历、后续遍历和二叉树还原
- 二叉树的遍历(先序、中序、后序)
- 二叉树的先序、中序、后序遍历
- 二叉树的遍历:先序、中序、后序
- 二叉树的先,中,后遍历方法
- 二叉树的先、中、后序遍历
- 二叉树的先序、中序、后序遍历
- 二叉树的先序、中序、后序遍历
- 二叉树的先序,中序,后序遍历
- 程序员的层级结构
- 百练_3248:最大公约数
- Map线程安全几种实现方法
- 首次曝光!在线视频衣物精确检索技术_开启刷剧败明星同款时代
- linux如何查看端口被哪个进程占用
- 二叉树的还原(先中 + 后中)
- NDEBUG 、DEBUG宏 和assert() 的用法
- ES6/ES2015核心内容讲解
- 【华为网络排错】ensp 错误代码:40,详细:启动失败
- 树莓派接口
- 云计算免费视频教程:Bashshell脚本编程详解
- SpringMvc 请求无法到达controller,出现404错误的原因
- 堡垒(fortress)
- linux常用命令三(备份压缩类)