后序遍历求解二叉树的高度
来源:互联网 发布:世界银行网站数据库 编辑:程序博客网 时间:2024/05/08 07:19
#include <stdio.h>#include <stdlib.h>#include <math.h>#define MAX 100 typedef struct BiTNode {char data;struct BiTNode *lchild,*rchild;}BiTNode,*BiTree;typedef struct {BiTree *base;int top;int stacksize;}SqStack;void InitStack(SqStack &S){//构造一个空栈 S.base=(BiTree *)malloc(MAX*sizeof(BiTree));if(!S.base)exit(0);S.top=0;S.stacksize=MAX;}void Push(SqStack &S,BiTree e){//入栈操作 if(S.top>=S.stacksize){S.base=(BiTree *)realloc(S.base,(MAX+10)*sizeof(BiTree));S.stacksize+=10;}S.base[S.top++]=e;}void Pop(SqStack &S,BiTree &e){//出栈操作 if(S.top==0)return;e=S.base[--S.top];}int StackEmpty(SqStack S){//栈的判空操作 if(S.top==0)return 1;return 0;}int GetTop(SqStack S,BiTree &e){//取栈顶元素 if(S.top==0)return 0;e=S.base[--S.top];return 1;}void CreatBiTree(BiTree &bt){//输入完全二叉树的先序序列,建立二叉树的二叉链表char ch;ch=getchar();if(ch=='#')bt=NULL;else{bt=(BiTree)malloc(sizeof(BiTNode));bt->data=ch;CreatBiTree(bt->lchild);CreatBiTree(bt->rchild);}}void PostOrderTraverse(BiTree bt){//利用后序遍历求解二叉树的高度int tmp=0;BiTree p,q;SqStack S;if(!bt){printf("0\n");return;}if(bt){InitStack(S);Push(S,NULL);p=bt;q=NULL;while(p || !StackEmpty(S)){if(p && p!=q){Push(S,p);if(S.top>tmp)//记录最大栈顶长度tmp=S.top;p=p->lchild;}else{Pop(S,p);if(!StackEmpty(S)){if(p->rchild && p->rchild!=q){Push(S,p);p=p->rchild;}else{//printf("%c ",p->data);q=p;}}}}printf("%d\n",tmp-1);//结点最大栈长即为二叉树的高度 }}int main(){BiTree bt;printf("输入完全二叉树的先序序列,建立二叉树的二叉链表:\n");CreatBiTree(bt);printf("所输入二叉树的高度是:");PostOrderTraverse(bt);return 0;}
0 0
- 后序遍历求解二叉树的高度
- 给出二叉树的先序和中序遍历,递归求解后序遍历
- java实现二叉树,以及二叉树的遍历和先序和后序求解
- 创建二叉树:层次遍历--树的宽度高度,后序遍历--祖先节点
- poj2255 根据任意二叉树的先序遍历和中序遍历求解后序遍历
- 利用二叉树的非递归后序遍历求解最近公共祖先问题
- 根据二叉树前序遍历和中序遍历序列求解后序遍历的算法
- 二叉树高度,栈实现二叉树的先序,中序,后序遍历的非递归遍历,二叉树层次遍历
- 求解二叉树高度的递归算法
- 通过前序(后序), 中序遍历建立二叉树 求解层次遍历
- 二叉树——已知前(后)序遍历和中序遍历,求解树
- 二叉树 前 后 中序遍历, 按层遍历, 求高度, 交换左右儿子等
- [二叉树专题]:递归求解二叉树的高度
- 二叉树遍历序列的求解
- 二叉树的遍历编程求解
- 二叉树BiTree的先序遍历.中序遍历.后序遍历.结点数.叶子结点数和数的高度
- 二叉树的后序遍历
- 二叉搜索树的后序遍历
- POJ 3044 City Skyline 可能会
- L2-011. 玩转二叉树
- Lesson 39 Am I all right?
- Jackson详细介绍
- SpringMVC 拦截器
- 后序遍历求解二叉树的高度
- 欢迎使用CSDN-Markdown编辑器写博客
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) D
- 图像拉普拉斯金字塔融合(Laplacian Pyramid Blending)
- goto anything
- Spring Security 自定义登陆页面报HTTP Status 403
- mongodb lock 出毛病时解决方法
- 一个ExtJs实例
- 管理后台的登录功能-重新思考