#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct Tree
{
char data;
struct Tree *l,*r;
}Tree;
void create(Tree *&T)
{
char c;
cin>>c;
if(c=='#')
{
T=NULL;
}
else
{
T=(Tree*)malloc(sizeof(Tree));
T->data=c;
create(T->l);
create(T->r);
}
}
char a;//指定字符
int sum=0;//度
//定义全局变量可以不用传参
void degree(Tree *T)
{
if(T!=NULL)
{
if(T->data==a)
{
if(T->l!=NULL)
{
sum++;
}
if(T->r!=NULL)
{
sum++;
}
cout<<sum;
}
else{
degree(T->l);
degree(T->r);
}
}
}
int main()
{
Tree *T;
create(T);
getchar();//读取换行
cin>>a;
degree(T);
return 0;
}
SWUSToj 1053 输出利用先序遍历创建的二叉树中的指定结点的度
最新推荐文章于 2024-06-15 11:44:36 发布