您的位置:首页 > 其它

求二叉树的叶节点

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;

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: