递归求二叉树的高度

本文探讨如何使用递归方法计算二叉树的高度。由于作者平时较少使用指针,因此在实现过程中遇到了一些挑战。
摘要由CSDN通过智能技术生成

用递归求树的高度,哎。。。平时很少用指针,写起来挺吃力的快哭了

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct bnode
{
	char data;
	struct bnode *lchild,*rchild;
}btnode;
int max(int x,int y)
{
	return x>y?x:y;
}
btnode *create()  //建树
{
	char c;
	btnode *t;
	c=getchar();
	if(c=='#')
	t=NULL;
	else
	{
		t=(btnode *)malloc(sizeof(btnode));
		t->data=c;
		t->lchild=create();
		t->rchild=create();
	}
	return t;
}
void print(btnode *t)
{
	if(t!=NULL)
	{
		printf("%c ",t->data);
		print(t->lchild);
		print(t->rchild);
	}
}
int high(btnode *T)
{
	if(T==NULL)
	return 0;
	else
	return max(high(T->lchild),high(T->rchild))+1;
}
int main()
{
	int ans;
	btnode *t;
	t=create();
	print(t);
	printf("\n");
	ans=high(t);
	printf("树的
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值