二叉树的深度

二叉树深度与平衡性判断算法详解
本文深入解析了二叉树的深度计算和平衡性判断算法,包括递归实现方法和后序遍历优化策略。通过具体实例演示,帮助读者理解并掌握关键概念。
/***************************************************************
题目一:输入一颗二叉树的根节点,求该数的深度。从根节点到叶节点依次
经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。
***************************************************************/
/*
解题思路:
树的深度等于(左右子树深度较大值+1),如果没有左右子树,则左右子树
深度设置为0;
*/
#include<stdio.h>
struct BinaryTreeNode
{
	int m_nValue;
	BinaryTreeNode* m_pLeft;
	BinaryTreeNode* m_pRight;
};

int treeDepath(BinaryTreeNode* pRoot)
{
	if(pRoot == NULL)
		return 0;
	int nLeft = treeDepath(pRoot->m_pLeft);
	int nRight = treeDepath(pRoot->m_pRight);

	return(nLeft>nRight ? nLeft+1 : nRight+1);
}
BinaryTreeNode* createTreeNode(int value)
{
	BinaryTreeNode* pNode = new BinaryTreeNode();
	pNode->m_nValue = value;
	pNode->m_pLeft = NULL;
	pNode->m_pRight = NULL;
	return pNode;
}
void connect(BinaryTreeNode* pParent, BinaryTreeNode* pLeftChild,
			 BinaryTreeNode* pRightChild)
{
	if(pParent == NULL)
		return;

	pParent->m_pLeft = pLeftChild;
	pParent->m_pRight = pRightChild;
}

void test()
{
	BinaryTreeNode* pNode1 = createTreeNode(1);
	BinaryTreeNode* pNode2 = createTreeNode(2);
	BinaryTreeNode* pNode3 = createTreeNode(3);
	BinaryTreeNode* pNode4 = createTreeNode(4);
	BinaryTreeNode* pNode5 = createTreeNode(5);
	BinaryTreeNode* pNode6 = createTreeNode(6);
	BinaryTreeNode* pNode7 = createTreeNode(7);

	connect(pNode1,pNode2,pNode3);
	connect(pNode2,pNode4,pNode5);
	connect(pNode5,pNode7,NULL);
	connect(pNode3,NULL,pNode6);

	int depath = treeDepath(pNode1);
	printf("%d\t",depath);
}
int main()
{
	test();
	return 0;
}


/***************************************************************
题目二:输入一颗二叉树的根节点,判断该树是不是平衡二叉树。如果
某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一颗平衡
二叉树。
***************************************************************/
/*
解题思路:
如果用前面提到的获取二叉树深度方法来遍历二叉树左右节点,以确定该二叉树
是否为平衡二叉树,这样会重复遍历一个节点多次。效率不高。
但如果我们用后序遍历的方式遍历二叉树的每一个节点,在遍历到一个节点之前
我们已经遍历了它的左右子树。只要在遍历每个节点的时候记录它的深度,我们
就可以一边遍历一边判断每个节点是不是平衡的。
*/
#include<stdio.h>
struct BinaryTreeNode
{
	int m_nValue;
	BinaryTreeNode* m_pLeft;
	BinaryTreeNode* m_pRight;
};

bool isBalanced(BinaryTreeNode* pRoot, int* pDepth)
{
	if(pRoot == NULL)
	{
		*pDepth = 0;
		return true;
	}

	int left, right;
	if(isBalanced(pRoot->m_pLeft,&left) 
		&& isBalanced(pRoot->m_pRight,&right))
	{
		int diff = left - right;
		if(diff <= 1 && diff >= -1)
		{
			*pDepth = (left > right ? left : right) + 1;
			return true;
		}
	}

	return false;
}
BinaryTreeNode* createTreeNode(int value)
{
	BinaryTreeNode* pNode = new BinaryTreeNode();
	pNode->m_nValue = value;
	pNode->m_pLeft = NULL;
	pNode->m_pRight = NULL;
	return pNode;
}
void connect(BinaryTreeNode* pParent, BinaryTreeNode* pLeftChild,
			 BinaryTreeNode* pRightChild)
{
	if(pParent == NULL)
		return;

	pParent->m_pLeft = pLeftChild;
	pParent->m_pRight = pRightChild;
}

void test()
{
	BinaryTreeNode* pNode1 = createTreeNode(1);
	BinaryTreeNode* pNode2 = createTreeNode(2);
	BinaryTreeNode* pNode3 = createTreeNode(3);
	BinaryTreeNode* pNode4 = createTreeNode(4);
	BinaryTreeNode* pNode5 = createTreeNode(5);
	BinaryTreeNode* pNode6 = createTreeNode(6);
	BinaryTreeNode* pNode7 = createTreeNode(7);

	connect(pNode1,pNode2,pNode3);
	connect(pNode2,pNode4,pNode5);
	connect(pNode5,pNode7,NULL);
	connect(pNode3,NULL,pNode6);

	int depath;
	if(isBalanced(pNode1,&depath)){
		printf("Yes");
	}
	
}
int main()
{
	test();
	return 0;
}


==参考剑指offer

计算二叉树深度主要有递归和非递归两种方法: ### 递归方法 递归计算二叉树深度的核心思想是分别计算左右子树的深度,然后取较大值加 1 作为当前树的深度。当遇到空树时,深度为 0。以下是递归计算二叉树深度的代码示例: ```c #include <stdio.h> // 定义二叉树节点结构体 typedef struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 递归计算二叉树深度的函数 int TreeDepth(TreeNode* root) { if (root == NULL) { return 0; // 空树深度为0 } int leftDepth = TreeDepth(root->left); // 计算左子树深度 int rightDepth = TreeDepth(root->right); // 计算右子树深度 return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1; // 返回较大深度加1 } ``` 这里定义了 `TreeNode` 结构体来表示二叉树节点,`TreeDepth` 函数通过递归调用自身计算左右子树深度,最后返回当前树的深度 [^1]。 ### 非递归方法 非递归方法通常使用队列来实现。其基本思路是通过层序遍历二叉树,每遍历完一层,深度加 1。以下是使用队列实现的非递归计算二叉树深度的代码示例: ```c #include <stdio.h> #define MAX_NODE 100 // 定义二叉树节点结构体 typedef struct BiTree { int data; struct BiTree* Lchild; struct BiTree* Rchild; } BiTree; // 非递归计算二叉树深度的函数 int Search_Depth(BiTree *T) { BiTree *Queue[MAX_NODE]; BiTree *p = T; int front = 0, rear = 0, depth = 0, levelLoc; // levelLoc 总是指向访问层的最后一个结点在队列的位置 if (T != NULL) { Queue[++rear] = p; // 根结点入队 levelLoc = rear; // 根是第1层的最后一个节点 } while (front < rear) { p = Queue[++front]; if (p->Lchild != NULL) { Queue[++rear] = p->Lchild; // 左结点入队 } if (p->Rchild != NULL) { Queue[++rear] = p->Rchild; // 右结点入队 } if (front == levelLoc) { // 访问到当前层的最后一个结点 depth++; levelLoc = rear; } } return depth; } ``` 这里定义了 `BiTree` 结构体表示二叉树节点,`Search_Depth` 函数使用队列 `Queue` 进行层序遍历,每遍历完一层,深度 `depth` 加 1,最后返回二叉树深度 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值