#include <iostream>
using namespace std;
typedef struct tree
{
char data;
tree* lchild, * rchild;
}Tree;
void Tree_creat(Tree*& root)
{
char ch;
cin >> ch;
if (ch != '#')
{
root = new Tree;
root->data = ch;
Tree_creat(root->lchild);
Tree_creat(root->rchild);
}
else
{
root = nullptr;
}
}
void count_deg(Tree* root,char data)
{
if (root)
{
if (root->data == data)
{
int deg = 0;
if (root->lchild)deg++;
if (root->rchild)deg++;
cout << deg;
return;
}
else
{
count_deg(root->lchild,data);
count_deg(root->rchild,data);
}
}
}
int main()
{
Tree* root = nullptr;
Tree_creat(root);
char data;
cin >> data;
count_deg(root, data);
return 0;
}
swust oj1053: 输出利用先序遍历创建的二叉树中的指定结点的度
最新推荐文章于 2024-06-15 16:17:24 发布