统计二叉树中只有左孩子的结点个数

Description

设二叉树的存储结构为二叉链表。在二叉链表中的每个结点由三部分组成:左孩子指针、右孩子指针和结点数据,其中如果一个结点的左右孩子不存在,则对应的指针记录为空,空指针用字符^占位。

Input

输入包括两部分:
第一部分,输入测试组数T(T<=10)
第二部分,T行 每行一棵非空的二叉树,每棵二叉树按先序遍历形式,空指针用字符^占位。
测试时,每棵二叉树不会超过20个结点。

Output

一个整数,代表该二叉树中只有左孩子的结点个数。

Sample Input
2
ABC^^^^
ABD^^E^^CF^^G^^

Sample Output
2
0

#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
typedef struct node{  
    char ch;  
    struct node *Lchild;  
    struct node *Rchild;  
}BiTNode,*BiTree;  
void Q_CreatTree(BiTree *T);  
int NodeNumber_1(BiTree T);
int NodeNumber_2(BiTree T);
int NodeNumber_0(BiTree T);
void Deepth(BiTree T,int *h);
int main(void)  
{  
	int n;
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		int h = 0; 
    	BiTree T;
		Q_CreatTree(&T);
		getchar();
		Deepth(T,&h);
		printf("%d\n",h);
	}
	return 0;
}  
void Q_CreatTree(BiTree *T)
{  
    char str;  
    str=getchar();  
    if(str=='^')  
    {  
        *T=NULL;  
    }  
    else  
    {  
        (*T)=(BiTree)malloc(sizeof(BiTNode));  
        (*T)->ch=str;  
        Q_CreatTree( &( (*T)->Lchild ) );  
        Q_CreatTree( &( (*T)->Rchild ) );  
    }  
}
void Deepth(BiTree T,int *h)
{
	if(T)
	{
		if(T->Lchild&&!T->Rchild)
		{
			(*h)+=1;
		}
		Deepth(T->Lchild,h);
		Deepth(T->Rchild,h);
	}
}
原创文章 382 获赞 114 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_45949073/article/details/105867951
今日推荐