算法--根据二叉树前序和中序遍历序列,求后续遍历序列
来源:互联网 发布:php设置中国上海时间 编辑:程序博客网 时间:2024/06/05 18:21
根据二叉树前序和中序遍历序列,求后续遍历序列。
#include<stdio.h>#include<string.h>#include<stdlib.h>//根据字符分割字符串void SplitSeq1(const char* srcSeq, char splitor, char* &left, char* &right){int len=strlen(srcSeq);int i=0;for(i=0; i<len; ++i){if(srcSeq[i]==splitor)break;}if(i==len){left=right=NULL;return;}left=(char*)malloc(sizeof(char)*(i+1));right=(char*)malloc(sizeof(char)*(len-i));int pos=i;for(i=0; i<pos; i++) left[i]=srcSeq[i];left[i]=0;for(i=pos+1; i<len; i++) right[i-pos-1]=srcSeq[i];right[len-pos-1]=0;}//分割字符串2void SplitSeq2(const char* srcStr, int len1, int len2, char* &left, char* &right){left=(char*)malloc(sizeof(char)*(len1+1));right=(char*)malloc(sizeof(char)*(len2+1));int pos=1, i=0;for( i=0; i<len1; i++)left[i]=srcStr[pos+i];left[i]=0;pos=pos+i;for(i=0; i<len2; i++)right[i]=srcStr[pos+i];right[i]=0;}void PrintPostSeq(const char* preSeq, const char* midSeq){if(*preSeq==0 || *midSeq==0) return;if(*midSeq==*preSeq && strlen(preSeq)==1){printf("%c", *preSeq); return;}char node=*preSeq;char *leftSeqMid=NULL, *rightSeqMid=NULL;SplitSeq1(midSeq, node, leftSeqMid, rightSeqMid);int len1=strlen(leftSeqMid); int len2=strlen(rightSeqMid);char *leftSeqPre=NULL, *rightSeqPre=NULL;SplitSeq2(preSeq, len1, len2, leftSeqPre, rightSeqPre);PrintPostSeq(leftSeqPre, leftSeqMid);PrintPostSeq(rightSeqPre, rightSeqMid);free(leftSeqPre); free(leftSeqMid);free(rightSeqPre); free(rightSeqMid); printf("%c", node);}int main(){char strPre[]="BCAD", strMid[]="CBAD";PrintPostSeq(strPre, strMid);return 0;}
0 0
- 算法--根据二叉树前序和中序遍历序列,求后续遍历序列
- 根据二叉树前序遍历和中序遍历序列求解后序遍历的算法
- 根据前序遍历序列和中序遍历序列构造二叉树算法
- 根据前序遍历序列和中序遍历序列构造二叉树算法
- 根据前序遍历序列和中序遍历序列构造二叉树算法
- 由中序遍历序列和后续遍历序列恢复二叉树
- 根据二叉树的前序遍历序列和中序遍历序列求二叉树的后序遍历序列
- 算法面试:根据前序遍历结果序列和中序遍历结果序列重构二叉树
- 根据二叉树的中序遍序列和后续遍历序列还原一颗二叉树
- 根据中序和层次遍历序列,构造二叉树
- 根据前序遍历序列和中序遍历序列构造二叉树
- 如何根据前序遍历序列和中序遍历序列确定二叉树
- 根据前序遍历序列和中序遍历序列重建二叉树
- 根据前序遍历序列和中序遍历序列构造二叉树
- 根据前序遍历序列和中序遍历序列重建二叉树
- 根据先序遍历序列和中序遍历序列重建二叉树
- 根据前序遍历序列和中序遍历序列创建二叉树
- 如何根据前序遍历序列和中序遍历序列确定二叉树
- 开发模式之策略模式
- 关于MFC中任意对象的拖拽功能的实现(COleDataSource, COleDropTarget)
- DICOM医学图像处理:Deconstructed PACS之Orthanc
- 如何下载各版本Chrome的离线安装包
- Spring连接数据库的方式1:利用Spring实现bean属性setter方式注入
- 算法--根据二叉树前序和中序遍历序列,求后续遍历序列
- HttpSessionListener ServletContextListener
- 第13周项目1-(5)
- 程序员的职业未来
- Linux信号机制
- JavaScript程序员必备的5个debug技巧
- 第一篇:WDF驱动学习之环境配置
- 学习习惯。
- FFT的详细解释,相信你看了就明白了。。。