二叉树叶子节点遍历---递归与非递归方法求取树深度

该博客探讨了如何通过递归和非递归方法遍历二叉树的叶子节点,并求取树的深度。首先,介绍了如何根据前序遍历序列创建二叉树,接着详细讲解了递归和非递归遍历叶子节点的算法。非递归方法利用栈来存储节点和深度信息,逐层遍历并更新最大深度。最后,实现了两种方法计算二叉树深度,并在示例中展示了它们的应用。
摘要由CSDN通过智能技术生成
#include <stdio.h>
#include <stdlib.h> 

#define A 18

//二叉树前序遍历序列,深度5,叶子节点
int buffer[20]={10,6,4,-1,-1,8,-1,-1,14,12,3,-1,4,-1,-1,-1,16,-1,-1,-100};

//二叉树结构体
typedef struct binary_tree_node
{
	int data;
	struct binary_tree_node* ltree;
	struct binary_tree_node* rtree;
}Btnode;

//创建新节点
Btnode* create_node(void)
{
	Btnode* node;
	node=(Btnode*)malloc(sizeof(Btnode));
	return node;
}

//据前序序列创建二叉树
/*
	明确问题:

	(1)何时进行二叉树分支的切换

		①左分支遍历到叶子节点时

		②右分支有新的节点加入时

	(2)何时节点入栈

		新加入的非空节点

	(3)何时节点出栈

		某分支遍历到叶子节点时
*/
Btnode* create_tree(int* buf)
{
	Btnode* root;
	Btnode* pnode,*temp;
	Btnode* s[A];
	bool ltree=true;
	int index=0;
	int m=0;
	
	root=create_node();
	root->data=buf[index++];
	s[m++]=root;
	pnode=root;

	while(buf[index]!=-100)
	{
		if(ltree==true)
		{
			if(buf[index]==-1)
			{
				pnode->ltree=NULL;
				index++;
				ltree=false;
				pnode=s[--m];
			}
			else
			{
				temp=create_node();
				temp->data=buf[index++];
				pnode->ltree=temp;
				s[m++]=temp;
				pnode=temp;
			}
		}
		else
		{
			if(buf[index]==-1)
			{
				pnode->rtree=NULL;
				index++;
				pnode=s[--m];
			}
			else
			{
				temp=create_node();
				temp->data=buf[index++];
				pnode->rtree=temp;
				s[m++]=temp;
				pnode=temp;
				ltree=true;
			}
		}
	}

	return root;
}

//遍历叶子节点
void leaf_traversal(Btnode* pnode)
{
	if(pnode!=NULL)
	{
		if(pnode->ltree==NULL && pnode->rtree==NULL)
		{
			printf("%d ",pnode->data);
		}
		leaf_traversal(pnode->ltree);
		leaf_traversal(pnode->rtree);
	}
	return;
}

typedef struct node_information
{
	Btnode* node;
	int depth;
}ND_INF;

//非递归法求取二叉树深度
/*
	基本思想:

	存储含有深度信息的节点,然后按二叉树的先序遍历进行
	
	入出栈操作,根据栈顶节点确定当前入栈节点的深度信息.

	出栈时使用temp保存出战节点的深度信息(用于右子树
	
	入栈时深度信息的获取)

	使用一变量进行二叉树深度信息的更新(即与当前的出栈
	
	节点的深度信息进行比较)
*/
void tree_depth(Btnode* root)
{
	ND_INF s[A];
	Btnode* pnode;
	int m=0,temp=0;
	int depth=0;
	bool ltree=true;

	for(int i=0;i<A;i++)
	{
		s[i].depth=0;
	}

	pnode=root;
	s[m].depth=1;
	s[m++].node=pnode;

	while(pnode!=NULL)
	{
		if(ltree==true)
		{
			if(pnode->ltree!=NULL)
			{
				pnode=pnode->ltree;	
				s[m].depth=s[m-1].depth+1;
				s[m++].node=pnode;
			}
			else
			{
				ltree=false;
			}
		}
		else
		{
			if(pnode->rtree!=NULL)
			{
				pnode=pnode->rtree;
				s[m].depth=temp+1;
				s[m++].node=pnode;
				ltree=true;
			}
			else
			{
				pnode=s[--m].node;
				temp=s[m].depth;
				if(s[m].depth>depth)
				{
					depth=s[m].depth;
				}
			}
		}
		if(m==-1)
		{
			break;
		}
	}

	printf("\n\nThe depth of the tree is %d (non-recursive method).\n\n",depth);
}

//递归方法求取二叉树的深度
int tree_depth1(Btnode* pnode)
{
	int dep1,dep2;

	if(pnode!=NULL)
	{
		dep1=tree_depth1(pnode->ltree);
		dep2=tree_depth1(pnode->rtree);
		return dep1>dep2 ? dep1+1 : dep2+1;
	}	

	return 0;
}

int main(void)
{
	Btnode* root;
	int depth=0;

	root=create_tree(buffer);
	printf("The leaf nodes in the tree are:\n");
	leaf_traversal(root);	
	tree_depth(root);

	depth=tree_depth1(root);
	printf("The depth of the tree is %d (recursive method).\n\n",depth);

	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值