求二叉树(二叉链表结构)的高度的三种方法

这篇博客介绍了三种不同的方法来计算二叉树的深度:非递归队列法、递归法和借助栈的后序遍历法。通过示例代码详细解释了每种方法的实现过程,并提供了测试环境进行验证。这些方法对于理解和处理二叉树的深度问题非常有帮助。
摘要由CSDN通过智能技术生成

算法一 

//非递归队列求二叉树深度 
int BTDepth(BiNode *T){
	if(!T) return 0;
	int front = -1, rear = -1;
	int last = 0;
	int level = 0;
	BiNode* Queue[maxsize];
	Queue[++rear] = T;
	BiNode *temp;
	while(front != rear){
		temp = Queue[++front];
		if(temp->lchild){
			Queue[++rear] = temp->lchild;
		}
		if(temp->rchild){
			Queue[++rear] = temp->rchild;
		}
		if(front == last){
			last = rear;
			level++;
		}
	}
	return level;
}

算法二 

int BTDepth1(BiNode *T){
	if(!T) return 0;
	int ldep = BTDepth1(T->lchild);
	int rdep = BTDepth1(T->rchild);
	if(ldep > rdep){
		return ldep + 1;
	}
	return rdep + 1;
}

算法三 

int BTDepth2(BiNode *T){
	if(!T) return 0;
	BiNode* stack[maxsize];
	int most = 0;
	int top = -1;
	BiNode* temp = (BiNode *)malloc(sizeof(BiNode));
	BiNode *p = temp;
	stack[++top] = T;
	while(top != -1){
		if(stack[top]->lchild && stack[top]->lchild != temp && stack[top]->rchild != temp){
			top++; 
			stack[top] = stack[top-1]->lchild;
		}
		else if(stack[top]->rchild && stack[top]->rchild != temp){
			top++;
			stack[top] = stack[top-1]->rchild;
		}
		else{ 
			if(top>most) most = top;
			temp = stack[top];
			top--;
		}
	}
	free(p);
	return most+1;	
}

 测试环境

#include<stdio.h> 
#include<stdlib.h>
#include<string.h>
#define maxsize 100
//二叉树的创建序列 
//ab#df###c#e##

typedef struct BiNode{
	char date;
	struct BiNode *lchild,*rchild;
}BiNode;

BiNode* CreateBiTree();
int BTDepth(BiNode *T);
int BTDepth1(BiNode *T);
int BTDepth2(BiNode *T);

int main(){
	BiNode* t = CreateBiTree();
	int most = BTDepth2(t);
	printf("%d",most);
	return 0;
}

// 借助栈后序遍历二叉树求二叉树的深度(栈达到最大高度即为树的深度) 
int BTDepth2(BiNode *T){
	if(!T) return 0;
	BiNode* stack[maxsize];
	int most = 0;
	int top = -1;
	BiNode* temp = (BiNode *)malloc(sizeof(BiNode));
	BiNode *p = temp;
	stack[++top] = T;
	while(top != -1){
		if(stack[top]->lchild && stack[top]->lchild != temp && stack[top]->rchild != temp){
			top++; 
			stack[top] = stack[top-1]->lchild;
		}
		else if(stack[top]->rchild && stack[top]->rchild != temp){
			top++;
			stack[top] = stack[top-1]->rchild;
		}
		else{ 
			if(top>most) most = top;
			temp = stack[top];
			top--;
		}
	}
	free(p);
	return most+1;	
}


//递归求二叉树的深度 
int BTDepth1(BiNode *T){
	if(!T) return 0;
	int ldep = BTDepth1(T->lchild);
	int rdep = BTDepth1(T->rchild);
	if(ldep > rdep){
		return ldep + 1;
	}
	return rdep + 1;
}
//非递归队列求二叉树深度 
int BTDepth(BiNode *T){
	if(!T) return 0;
	int front = -1, rear = -1;
	int last = 0;
	int level = 0;
	BiNode* Queue[maxsize];
	Queue[++rear] = T;
	BiNode *temp;
	while(front != rear){
		temp = Queue[++front];
		if(temp->lchild){
			Queue[++rear] = temp->lchild;
		}
		if(temp->rchild){
			Queue[++rear] = temp->rchild;
		}
		if(front == last){
			last = rear;
			level++;
		}
	}
	return level;
}
//递归方法中序创建二叉树 
BiNode* CreateBiTree(){
	BiNode *p;
	char a;
	scanf("%c",&a);
	if(a=='#')
	p=NULL;
	else{
		p=(BiNode *)malloc(sizeof(BiNode));
		p->date=a;
		p->lchild=CreateBiTree();
		p->rchild=CreateBiTree();
	}	
	return p;
}
//递归方法中序遍历二叉树 
void Traverse(BiNode *p){
	if(p==NULL){
		return;
	}
	else{
		Traverse(p->lchild);
		printf("%c",p->date);
		Traverse(p->rchild);
	}
	return;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值