二叉树深度

在这里插入代码片
#include "stdio.h"
#include "malloc.h"
typedef struct node{
	char data;
	struct node *lchild,*rchild;
}Tree ,*btree;
void creat(btree *t){
	char a;
	scanf("%c",&a);
	if(a==' '){
		*t=NULL;
	}else{
		*t=(btree)malloc(sizeof(btree));
		(*t)->data=a;
		creat(&((*t)->lchild));
		creat(&((*t)->rchild));
	}
}

//先序遍历:根节点,左子树,右子树 
void display(btree t,int len){
	if(t){
		printf("%c位于第%d层\n",t->data,len);
		qian(t->lchild,len+1);
		qian(t->rchild,len+1);
	}
}
//二叉树深度
int m,n;//全局变量
int depth(btree t){
	if(t==NULL){
		return 0;
	}else{
		m=depth(t->lchild);
		n=depth(t->rchild);
		if(m>n){
			return(m+1);
		}else{
			return(n+1);
		}
	}
}
int main(){
	btree t=NULL,t1;
	int len=1,l;
	creat(&t);
	printf("二叉树:\n");
	printf("输出:\n");
	display(t,len);
	printf("\n");
	l=depth(t);
	printf("二叉树深度:%d\n",l);
	printf("\n");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值