数据结构与算法-头歌【5】二叉树--课上练
·
任务描述
本关任务:编写一个能计算二叉树中有两个孩子的满结点个数。
相关知识
参考第一关中的相关知识。
编程要求
请仔细阅读右侧编辑器中的框架代码,在Begin-End之间补充代码,完成int countFullNode(BiTree root)函数计算二叉树中满结点的个数。
代码
#include <iostream>
using namespace std;
typedef char DataType;
char count = 0;
//二叉树数据结构
struct node
{
DataType info ; //存放结点数据
struct node *lchild , *rchild ; //指向左右孩子的指针
};
typedef struct node *BiTree ;
/*创建二叉树
函数名:createBiTree
参数:无
返回值:二叉树根结点指针
*/
BiTree createBiTree(void)
{
char ch ;
BiTree root ;
cin>>ch ;
if(ch == '#') root = NULL;
else{
root = new struct node ;
root->info = ch ;
root->lchild = createBiTree() ;
root->rchild = createBiTree();
}
return root ;
}
void visit(BiTree T)
{
cout<<T->info ;
}
int countFullNode(BiTree root)
{
//请在此处填写代码,计算二叉树中满结点的个数
/********** Begin **********/
if(root != NULL)
{
if(root->lchild != NULL && root->rchild != NULL) count++;
countFullNode(root->lchild);
countFullNode(root->rchild);
}
return count;
/*********** End-**********/
}
int main(void)
{
BiTree root = createBiTree();
cout<<countFullNode(root) ;
}
更多推荐
所有评论(0)