二叉树 (求叶子结点&深度)
来源:互联网 发布:韩国人眼中的美女知乎 编辑:程序博客网 时间:2024/04/30 02:51
题目描述
已知一个按先序序列输入的字符序列,如abc,,de,g,,f,,,(其中逗号表示空节点)。请建立二叉树并按中序和后序方式遍历二叉树,最后求出叶子节点个数和二叉树深度。
输入
输入一个长度小于50个字符的字符串。
输出
输出共有4行:
第1行输出中序遍历序列;
第2行输出后序遍历序列;
第3行输出叶子节点个数;
第4行输出二叉树深度。
示例输入
abc,,de,g,,f,,,示例输出
cbegdfa
cgefdba
3
5
#include<stdio.h>#include<stdlib.h>#include<string.h>struct node{ char data;struct node *left;struct node *right;};struct node *make()//建树{ char c; struct node *str; c = getchar(); if(c == ',') str = NULL; else { str=(struct node *)malloc(sizeof(struct node)); str->data = c; str->left = make(); str->right = make(); } return str;}void mid(struct node *mid1)//中序{ if(mid1!=NULL) { mid(mid1->left); printf("%c",mid1->data); mid(mid1->right); } else return ;}void last(struct node *last1)//后序{ if(last1!= NULL) { last(last1->left); last(last1->right); printf("%c",last1->data); } else return ;}int count = 0;void leaf(struct node *t)//叶子结点数{ if(t!=NULL ) { if(t->left==NULL && t->right==NULL) { count ++; } leaf(t->left); leaf(t->right); }}int shendu(struct node *t)//深度{ int a = 0; int b = 0; if(t!=NULL) { a = shendu(t->left); b = shendu(t->right); if(a > b) return a + 1; //层层累加过程,直到root结点 else return b + 1; } else return 0;}int main(){ struct node *t; t = make(); mid(t); printf("\n"); last(t); printf("\n"); leaf(t);printf("%d\n",count);count = 0;int du = shendu(t); printf("%d\n",du); return 0;}
0 0
- 二叉树 (求叶子结点&深度)
- 二叉树(求深度、叶子结点)
- 求二叉树的深度、宽度和叶子结点数
- 求二叉树的叶子结点个数及深度
- 二叉树的基本操作精集(创建、遍历、求深度结点以及叶子结点个数)
- 求二叉树的叶子结点数
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- C二叉树的基本操作---创建、遍历、求深度、求叶子结点
- C语言实现二叉树的基本操作---创建、遍历、求深度、求叶子结点
- 二叉树的操作:创建、遍历、求树的深度、叶子结点数
- 输入二叉树,求父,左,右结点和深度,叶子数
- 二叉树(二)——遍历、深度统计、叶子结点统计、结点统计
- 二叉树求深度和叶子数(20 分)
- 二叉树的三种遍历的应用(表达式,求深度,叶子数,结点数,二叉树的建立,复制)
- DS之求解二叉树的叶子结点和深度
- 7-1 求二叉树的叶子结点个数
- 先序,中序,后序,求叶子结点数,深度,拷贝,几种二叉树的常见递归使用方法
- 设一棵完全二叉树共有700个结点,求该二叉树有几个叶子结点?
- oracle数据库
- 电子电路分析方法
- 支持向量机 (SVM)从判定结果开始看
- Core Data
- Linux的IO性能监控工具iostat详解
- 二叉树 (求叶子结点&深度)
- Android Activity为什么要细化出onCreate、onStart、onResume、onPause、onStop、onDesdroy这么多方法让应用去重载?
- 平均数 The Average
- 需要学习的单词
- DWZ中Tab组件实现多行选项卡
- 分析u-boot中的“.word”伪指令
- 设计模式(1)——单例模式
- LA 2678 UVA 1121 - Subsequence
- java 左填充