前序中序/后序中序确定二叉树
来源:互联网 发布:并行计算的编程模型 编辑:程序博客网 时间:2024/06/06 05:00
这个代码网上不要太多了,考研复习,贴出比较容易理解的代码,但是写得丑(只是写了比较完整的思路,你看我类型定义都没定义呢)。。。
前序、中序确定二叉树
void preInCreat(Tree r,ElemType a[],ElemType b[],int as,int ad,int bs,int bd){ //as:A数组第一个元素的下标,as:A数组最后一个元素下标,bs,as,bs初值为1 if(as>ad) return; r=(treeNode*)malloc(sizeof(treeNode)); r->data=a[as]; int i; for(i=bs;b[i]!=r->data;i++) int llenth=i-bs;//左子树长度 int rlenth=bd-i;//右子树长度 if(llenth) preInCreat(r->lchild,a,b,as+1,as+llenth,bs,bs+llen-1); else r->lchild=NULL; if(rlenth) preInCreat(r->rchild,a,b,ad-rlen+1,ad,bd-rlenth+1,bd);//关于下标问题一定要画画图搞搞清楚 else r->rchild=NULL;}
后序、中序确定二叉树
void postInCreat(Tree r,ElemType a[],ElemType b[],int as,int ad,int bs,int bd){ if(as>ad) return; r=(treeNode*)malloc(sizeof(treeNode)); r->data=a[ad];//后序遍历根结点是最后一个元素 int i; for(i=bs;b[i]!=r->data;i++) int llenth=i-bs; int rlenth=bd-i; if(llenth) postInOrder(r->lchild,a,b,as,as+llenth-1,bs,bs+llenth-1); else r->lchild=NULL; if(rlenth) postInOrder(r->rchild,a,b,ad-rlenth,ad-1,bd-r+1,bd); else r->rchild=NULL;}
阅读全文
0 0
- 前序中序/后序中序确定二叉树
- 根据前序中序或后序中序确定并创建二叉树
- 确定二叉树结构
- 先序、中序确定二叉树
- 根据遍历序列确定二叉树
- 如何确定并重构一棵二叉树
- 二叉树前序、中序确定树结构
- 二叉树——中后序确定
- 根据遍历序列确定二叉树
- 二叉树-dfs确定路径个数
- 根据二叉树的前中序确定唯一的二叉树
- 确定一个二叉树是另一个二叉树的子树
- 重建二叉树-确定唯一的二叉树
- 二叉树根据前序和中序确定二叉树
- 确定后序遍历二叉树是否构成二叉排序树
- 回顾二叉树,采用增加线索确定根节点
- Tree Recovery(过遍历确定二叉树结构)
- 根据先中序序列或后中序序列确定二叉树
- Spring源码学习——AbstractApplicationContext
- Android新特性介绍,ConstraintLayout完全解析
- Oracle学习笔记(六)
- SceneLayer的见解
- form表单内容
- 前序中序/后序中序确定二叉树
- HDU 2180 时钟
- dubbo 加载Bean和远程调用分析(1)
- 11款热门PHP集成开发环境
- javac编译Servlet类出现找不到javax.servlet包的情况
- jdk自带线程池详解
- Data structure-3 单链表LinkedList--Java语言实现
- 杭电OJ1005题
- 算法导论5.2-4