#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;
}
求二叉树的叶节点
最新推荐文章于 2023-04-17 19:19:42 发布