求二叉树的叶节点
2016-04-16 17:07
232 查看
#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;
};
void CreatBiTree(BiTNode* *T) //创建二叉树必须得是二级指针
{
char c;
cin >> c;
if (c == '#') *T = NULL;
else
{
*T = (BiTNode*)malloc(sizeof(BiTNode));
(*T)->data = c;
CreatBiTree(&((*T)->lchild));
CreatBiTree(&((*T)->rchild));
}
}
void GetLeaver(BiTNode* T, int *count) //count是在main函数中定义的,这样可以在main函数中看到结果
{
/*if (T->lchild == NULL && T->rchild == NULL && T != NULL)
{
*count = *count + 1;
}*/
if (T)
{
if (T->lchild == NULL && T->rchild == NULL)*count = *count + 1;
GetLeaver(T->lchild, count);
GetLeaver(T->rchild, count);
}
}
int main()
{
int count = 0;
BiTNode* T = NULL;
CreatBiTree(&T);
GetLeaver(T, &count);
cout << "the number of leaves of btree are " << count << endl;
return 0;
}
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;
};
void CreatBiTree(BiTNode* *T) //创建二叉树必须得是二级指针
{
char c;
cin >> c;
if (c == '#') *T = NULL;
else
{
*T = (BiTNode*)malloc(sizeof(BiTNode));
(*T)->data = c;
CreatBiTree(&((*T)->lchild));
CreatBiTree(&((*T)->rchild));
}
}
void GetLeaver(BiTNode* T, int *count) //count是在main函数中定义的,这样可以在main函数中看到结果
{
/*if (T->lchild == NULL && T->rchild == NULL && T != NULL)
{
*count = *count + 1;
}*/
if (T)
{
if (T->lchild == NULL && T->rchild == NULL)*count = *count + 1;
GetLeaver(T->lchild, count);
GetLeaver(T->rchild, count);
}
}
int main()
{
int count = 0;
BiTNode* T = NULL;
CreatBiTree(&T);
GetLeaver(T, &count);
cout << "the number of leaves of btree are " << count << endl;
return 0;
}
相关文章推荐
- 第四次_Activity传递参数
- 基于Flume的美团日志收集系统(二)改进和优化
- leetcode——40——Combination Sum II
- 网络流二十四题之三 —— 最小路径覆盖问题(PATH)
- linux 下手动安装 gcc
- 基于Flume的美团日志收集系统(一)架构和设计
- TCP/IP(3)
- HDU 2553 N皇后问题(回溯 + 剪枝)
- 序列化二叉树
- 适配器模式的分析、结构图及基本代码
- hdu 5115 Dire Wolf【区间dp】
- 关于图片的等比缩放,从相机和照片选取头像,显示圆形头像
- ios申请真机调试( xcode 5)详细解析
- beanshell 线上修复BUG
- 【问题】Failed to send producer request with correlation id 2 to broker 0 with
- 大学毕业4年-回顾和总结(6)-技术研发-重构之法
- 大学毕业4年-回顾和总结(6)-技术研发-重构之法
- c3p0数据库连接池配置
- Game of Hyper Knights (SG函数)
- 大学毕业4年-回顾和总结(6)-技术研发-重构之法