(p138)递归完成树的遍历
来源:互联网 发布:随机信号分析 知乎 编辑:程序博客网 时间:2024/06/02 01:15
递归还是很简单的,分分钟写出来,可是非递归那就有点难了,加油加油!下面是递归的代码:
/* * source.c * * Created on: Feb 22, 2016 * Author: wing */#include<stdio.h>#include<stdlib.h>#define max 10/*二叉树节点数目*/struct node{int n;struct node *p,*l,*r;};int presearch(struct node *p)/*先序遍历*/{printf("%d ",p->n);if (p->l!=NULL)presearch(p->l);if (p->r!=NULL)presearch(p->r);return 0;}int possearch(struct node *p)/*后序遍历*/{if (p==NULL)return 0;possearch(p->l);possearch(p->r);printf("%d ",p->n);return 0;}int insearch(struct node *p)/*中序遍历*/{if (p==NULL)return 0;insearch(p->l);printf("%d ",p->n);insearch(p->r);return 0;}int build(struct node *root,int n)/*由一个x[i]=i的数组建立二叉树,方法和建堆的方法一样*/{int i;for (i=1;i<=n;i++)root[i].n=i;for (i=1;i<=n/2;i++){root[i].p=root+i/2;if (i*2<=n)root[i].l=root+i*2;elseroot[i].l=NULL;if (i*2+1<=n)root[i].r=root+i*2+1;elseroot[i].r=NULL;}return 0;}int main(void){struct node *root;root=(struct node *)malloc(sizeof(struct node)*(max+1));build(root,max);printf("先序遍历: ");presearch(root+1);printf("\n后序遍历: ");possearch(root+1);printf("\n中序遍历: ");insearch(root+1);return 0;}
0 0
- (p138)递归完成树的遍历
- (p138)非递归完成二叉树的遍历
- (p138)只用固定量额外存储空间不用递归遍历二叉树
- 今天终于完成自己的第一个算法(非递归遍历二叉树)
- 利用非递归方法实现二叉树的后序遍历 .(待完成!!!!!!)
- 利用非递归方法实现二叉树的层次遍历 .(待完成!!!!!!)
- 二叉树的先中后序遍历,递归遍历,非递归遍历
- 树的建立 递归非递归遍历
- 递归非递归实现树的遍历
- 二叉树的递归,非递归遍历
- 树的递归创建和递归遍历
- 二叉树的递归+非递归遍历
- 树的遍历----递归与非递归
- 树的递归和非递归遍历
- 二叉树的递归非递归遍历
- 二叉树的遍历--递归+非递归
- 二叉树的递归、非递归遍历
- 树的遍历(递归、非递归)
- 【慕课笔记】第四章 JAVA中的集合框架(上) 第6节 学生选课—课程查询
- android adapter 操作Activity中的方法 ------------弱引用WeakReference----断点记录
- hadoop学习之路(一)hadoop集群服务搭建
- 构建基于阿里云OSS的第三方文件上传RESTful接口
- iOS 设备通用设备适配
- (p138)递归完成树的遍历
- 变相自定义input标签file类型样式
- Log4j扩展使用--日志记录器Logger
- 使用 markdownj 将 markdown 转换为 html
- java中final初探
- Windows中的基本概念和术语
- UVALive 5870 - Smooth Visualization
- 韩顺平视频迅雷下载
- MyBatis的动态SQL详解