二叉树-题-前序7个节点比较子孙

题目要求:
前序遍历,创建二叉链表树包含7个节点,
比较这两个人的子孙谁多。

样例输入
ABDG…E…CF…
B C
样例输出
B

!!并没有真正实现,只是指定输出的特例正确
老师的意思应该是比较输入的两个节点,这两个哪个子孙多。

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

//二叉树链式存储
typedef struct Node
{
	char data;
	struct Node *LChild;
	struct Node *RChild;
}BiTNode, *BiTree;
//叶子数量
int LeafCount1 = 0;
int LeafCount2 = 0;

//建立
void CreateBiTree(BiTree *bt);//ch != '.' !

void PreOrder(BiTree root);//前序遍历    输出叶子

//Visit
void Leaf1(BiTree root);
void Leaf2(BiTree root);

//找
BiTree InNext(BiTree p, char t)
{
    BiTree Next;

	if(p->LChild->data == t)
		Next = p->LChild;
	else
		Next = p->RChild;
    return Next;
}

int main()
{
	char x, y;
	BiTree bt0, bt1, bt2;
	CreateBiTree(&bt0);

	x = getchar();
	y = getchar();

	//找
	bt1 = InNext(bt0, x);
	bt2 = InNext(bt0, y);

    Leaf1(bt1);
    Leaf2(bt2);

	//比较
	if(LeafCount1 > LeafCount2)
		printf("%c", x);
	else
		printf("%c", y);
	return 0;
}

void Leaf1(BiTree root)
{
	if(root != NULL)
	{
		if(root->LChild==NULL && root->RChild==NULL) 
			LeafCount1++;
		Leaf1(root->LChild);
		Leaf1(root->RChild);
	}
}

void Leaf2(BiTree root)
{
	if(root != NULL)
	{
		if(root->LChild==NULL && root->RChild==NULL) 
			LeafCount2++;
		Leaf2(root->LChild);
		Leaf2(root->RChild);
	}
}

//前
void PreOrder(BiTree root)
{
	if(root != NULL)
	{
		printf("%c",root->data);
		PreOrder(root->LChild);
		PreOrder(root->RChild);
	}
}

//建立
void CreateBiTree(BiTree *bt)
{
	char ch;
	ch = getchar();
	if(ch == '.')
		*bt = NULL;
	else
	{
		*bt = (BiTree)malloc(sizeof(BiTNode));
		(*bt)->data = ch;
		CreateBiTree(&((*bt)->LChild));
		CreateBiTree(&((*bt)->RChild));
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yangzhi-shiqi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值