重建二叉树
来源:互联网 发布:怎么制作mac系统u盘 编辑:程序博客网 时间:2024/04/30 21:08
二叉树的节点为:
struct BiTNode{TElemType data;struct BiTNode *leftchild,*rightchild;};
创建节点统一由Buynode() 函数完成:
BiTNode * Buynode(){BiTNode *s=new BiTNode();if(s == NULL){exit(1);}memset(s,0,sizeof(BiTNode));return s;}
一,由前序和中序遍历的字符串创建二叉树,返回二叉树头指针。
BiTNode * PreInCreateTree(char *s1,char *s2,int n)//n标识s2的长度{if(n == 0){return NULL;}int i=0;while(*s1 != s2[i]){++i;}BiTNode *h=Buynode();h->data=*s1;h->leftchild=PreInCreateTree(s1+1,s2,i);h->rightchild=PreInCreateTree(s1+i+1,s2+i+1,n-i-1);return h;}二,由中序和后序遍历的字符串创建二叉树,返回二叉树头指针。
BiTNode * InPostCreateTree(char *s1,char *s2,int n){if(n == 0){return NULL;}int i=n-1;while(s2[n-1] != s1[i]){--i;}BiTNode *h=Buynode();h->data=s2[n-1];h->leftchild=InPostCreateTree(s1,s2,i);h->rightchild=InPostCreateTree(s1+i+1,s2+i,n-i-1);return h;}
//时间原因在这里不做实现方法的解释
//测试用例
char *s1="ABCDEFGH";char *s2="CBEDFAGH";
char *s3="CEFDBHGA";
- 3.9重建二叉树
- 重建二叉树
- 二叉树重建
- 二叉树重建
- 二叉树的重建
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 重建二叉树
- 二叉树重建
- 二叉树重建
- 重建二叉树
- 重建二叉树
- 重建二叉树
- CSS position 属性
- JdbcTemplate学习笔记
- Extjs tree的相关组件及属性
- Extjs 设置控件 z-index 层次
- Delphi 释放内存
- 重建二叉树
- sql 2008评估期已过处理方案
- 老公与外面的朋友合伙做生意让自己很纠结
- oracle 手工创建Scott用户
- Extjs floating Panel 挡住 提示框或确认框的解决方法
- ByteArrayOutputStream用法
- 汤姆大叔深入理解JavaScript系列
- Extjs 双击关闭tab页
- struts2的s:iterator 标签 详解