【C++数据结构】二叉树 节点数量 叶子节点数量

【问题描述】

在卡卡的房子外面,有一棵苹果树。每年的春天,树上总会结出很多的苹果。卡卡非常喜欢吃苹果,所以他一直都精心的呵护这棵苹果树,让这棵苹果树分的叉不多于2叉,苹果会长在枝条的分叉点上面,且不会有两个苹果结在一起。卡卡给这棵苹果树上的所有苹果都编了字母号码,如图所示。

在这里插入图片描述
你能告诉卡卡,他的苹果树上一共有多少个苹果吗?
卡卡想知道有多少个苹果叉不再分叉的,你能告诉他吗?

【输入形式】

ABD#G###CE##F##

【输出形式】

苹果数
【样例输入】

ABD#G###CE##F##
【样例输出】

7

#include<iostream>
//#include<string>
using namespace std;

static int count = 0;
# define Maxsize 20

struct Bitree {
	char data;
	Bitree* left;
	Bitree* right;
};

void creat(Bitree* &node)
{
	char c;
	cin >> c;
	if (c == '#')
		node = NULL;
	else
	{
		node = new Bitree;
		node->data = c;
		creat(node->left);
		creat(node->right);
	}
}

void visit(Bitree* &node)
{
	cout << node->data;
}

void xianxu(Bitree* &node)
{
	if (node == NULL)
		return;
	visit(node);
	xianxu(node->left);
	xianxu(node->right);
}

void houxu(Bitree*& node)
{
	if (node == NULL)
		return;
	xianxu(node->left);
	visit(node);
	xianxu(node->right);
}

void dancha(Bitree* &node,int &count)
{
	if (node == NULL)
		return;
	//visit(node);
	if ((node->left == NULL && node->right != NULL) || (node->left != NULL && node->right == NULL))
		count++;
	dancha(node->left,count);
	dancha(node->right,count);
}

void liangcha(Bitree*& node, int &two)
{
	if (node == NULL)
		return;
	//visit(node);
	if (node->left != NULL && node->right != NULL)
		two++;
	liangcha(node->left, two);
	liangcha(node->right, two);
}

void many(Bitree* &node, int& num)
{
	if (node == NULL)
	{
		return;
	}
	num++;
	many(node->left,num);
	many(node->right,num);
}

void nofencha(Bitree*& node, int& num)
{
	if (node == NULL)
	{
		return;
	}
	if (node->left == NULL && node->right == NULL)
		num++;
	nofencha(node->left, num);
	nofencha(node->right, num);
}

int main()
{
	Bitree* root;
	creat(root);

	int count = 0;
	//dancha(root,count);
	//cout << count << endl;
	//int two = 0;
	//liangcha(root, two);
	//cout << two;
	int num = 0;
	many(root, num);
	cout << num<<endl;
	int no = 0;
	nofencha(root, no);
	cout << no;



	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值