【问题描述】
在卡卡的房子外面,有一棵苹果树。每年的春天,树上总会结出很多的苹果。卡卡非常喜欢吃苹果,所以他一直都精心的呵护这棵苹果树,让这棵苹果树分的叉不多于2叉,苹果会长在枝条的分叉点上面,且不会有两个苹果结在一起。卡卡给这棵苹果树上的所有苹果都编了字母号码,如图所示。
卡卡想知道有多少个苹果叉是单叉/两叉的,你能告诉他吗?
【输入形式】
ABD#G###CE##F##
【输出形式】
单叉苹果叉的个数
【样例输入】
ABD#G###CE##F##
【样例输出】
2
#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);
}
int main()
{
Bitree* root;
creat(root);
int count = 0;
dancha(root,count);
cout << count << endl;
int two = 0;
liangcha(root, two);
cout << two;
return 0;
}