(p138)只用固定量额外存储空间不用递归遍历二叉树
来源:互联网 发布:sql查询分析执行 编辑:程序博客网 时间:2024/06/06 11:39
我写过最搞笑的程序:基本思路就是记录从哪个方向到达节点,很多都是复制粘贴^-^
/* * source.c * * Created on: Feb 25, 2016 * Author: wing *//* * source.c * * Created on: Feb 25, 2016 * Author: wing */#include<stdio.h>#include<stdlib.h>#define maxsize 1000struct node{char n;struct node *p,*l,*r;};struct stack{struct node **head;int top;};int build(struct node **parent)/*递归建立二叉树*/{char n;scanf("%c",&n);if (n=='#'){*parent=NULL;return 0;}else{*parent=(struct node *)malloc(sizeof(struct node));(*parent)->n=n;build(&(*parent)->l);if ((*parent)->l!=NULL)(*parent)->l->p=*parent;build(&(*parent)->r);if ((*parent)->r!=NULL)(*parent)->r->p=*parent;return 0;}}int initstack(struct stack *st){st->head=(struct node**)malloc(sizeof(struct node*)*(maxsize));st->top=-1;return 0;}int push(struct stack *st,struct node *next){st->head[++(st->top)]=next;return 0;}struct node *pop(struct stack *st){return st->head[st->top--];}int pretrav(struct node *root){struct node *next=root;int dir=1;while (next!=NULL){if(dir==1){printf("%c",next->n);if (next->l!=NULL){next=next->l;dir=1;}elseif (next->r!=NULL){next=next->r;dir=1;}else{if (next->p==NULL){next==NULL;break;}if (next==next->p->l)dir=2;elsedir=3;next=next->p;}}if (dir==2)if (next->r!=NULL){next=next->r;dir=1;}else{if (next->p==NULL){next=NULL;break;}if (next==next->p->l)dir=2;elsedir=3;next=next->p;}if (dir==3){if (next->p==NULL){next=NULL;break;}if (next==next->p->l)dir=2;elsedir=3;next=next->p;}}return 0;}int main(void){struct node **root;root=(struct node **)malloc(sizeof(struct node*));build(root);printf("先序遍历:");pretrav(*root);return 0;}
0 0
- (p138)只用固定量额外存储空间不用递归遍历二叉树
- 遍历二叉树,利用栈和只用固定存储空间,递归和非递归。
- (p138)非递归完成二叉树的遍历
- (p138)递归完成树的遍历
- 二叉树非递归使用常数存储空间遍历
- 不用栈实现二叉树非递归中序遍历
- 不用栈实现二叉树非递归中序遍历
- parent指针非递归不用栈中序遍历二叉树
- 不用递归和辅助空间对二叉树进行遍历
- 二叉搜索树(遍历不递归,不用栈)
- 只用一个栈实现二叉树的非递归后序遍历
- 线索二叉树 Threaded Binary Tree (不用递归,不用栈,遍历二叉树)
- GridView固定表头(不用javascript只用CSS)
- GridView固定表头(不用javascript只用CSS)?
- C++实现二叉树 前序遍历, 后序遍历, 中序遍历, 层序遍历(不用递归)
- 二叉树递归遍历
- 二叉树遍历(递归)
- 二叉树递归遍历
- Day2(移动开发基础-html标签+安卓入门)
- 哈理工OJ 2170 大斐波那契数(大数加法)
- Java的命名习惯
- HDU 1698 Just a Hook(线段树:区间set,区间查询)
- 根据模型中的参数数量判断需要的训练数据的数量
- (p138)只用固定量额外存储空间不用递归遍历二叉树
- new与malloc的区别
- 随笔jar
- Generic
- Linux知识积累(长更)
- 最短路径
- 155. Min Stack&包含min函数的栈
- android提示Field can be converted to a local varible
- bc13&&hdoj5062&&hdoj5063