当前位置:Gxlcms > mysql > 求一个二叉树中任意两个节点间的最大距离,两个节点的距离的定义

求一个二叉树中任意两个节点间的最大距离,两个节点的距离的定义

时间:2021-07-01 10:21:17 帮助过:40人阅读

题目: 求一个二叉树中任意两个节点间的最大距离,两个节点的距离的定义是这两个节点间边的个数,比如某个孩子节点和父节点间的距离是1,和相邻兄弟节点间的距离是2, 优化时间空间杂度。 思路一: 计算一个二叉树的最大距离有两个情况: 情况A: 路径经过左子

题目:
求一个二叉树中任意两个节点间的最大距离,两个节点的距离的定义是这两个节点间边的个数,比如某个孩子节点和父节点间的距离是1,和相邻兄弟节点间的距离是2,

优化时间空间杂度。

思路一:

计算一个二叉树的最大距离有两个情况:
情况A: 路径经过左子树的最深节点,通过根节点,再到右子树的最深节点。
情况B: 路径不穿过根节点,而是左子树或右子树的最大距离路径,取其大者。
首先算出经过根节点的最大路径的距离,其实就是左右子树的深度和;然后分别算出左子树和右子树的最大距离,三者比较,最大值就是当前二叉树的最大距离了。

代码如下:

[cpp] view plaincopyprint?

  1. /*-----------------------------
  2. Copyright by yuucyf. 2011.09.02
  3. ------------------------------*/
  4. #include "stdafx.h"
  5. #include
  6. #include
  7. using namespace std;
  8. typedef struct tagSBTreeNode
  9. {
  10. tagSBTreeNode *psLeft;
  11. tagSBTreeNode *psRight;
  12. int nValue;
  13. int nMaxLeft;
  14. int nMaxRight;
  15. tagSBTreeNode()
  16. {
  17. psLeft = psRight = NULL;
  18. nValue = 0;
  19. nMaxLeft = nMaxRight = 0;
  20. }
  21. }S_TreeNode;
  22. void AddTreeNode(S_TreeNode *&psTreeNode, int nValue)
  23. {
  24. if (NULL == psTreeNode)
  25. {
  26. psTreeNode = new S_TreeNode;
  27. assert(NULL != psTreeNode);
  28. psTreeNode->nValue = nValue;
  29. }
  30. else if (psTreeNode->nValue < nValue)
  31. {
  32. AddTreeNode(psTreeNode->psRight, nValue);
  33. }
  34. else
  35. AddTreeNode(psTreeNode->psLeft, nValue);
  36. }
  37. int MaxDepth(const S_TreeNode *psTreeNode)
  38. {
  39. int nDepth = 0;
  40. if (NULL != psTreeNode)
  41. {
  42. int nLeftDepth = MaxDepth(psTreeNode->psLeft);
  43. int nRightDepth = MaxDepth(psTreeNode->psRight);
  44. nDepth = (nLeftDepth > nRightDepth) ? nLeftDepth : nRightDepth;
  45. nDepth++;
  46. }
  47. return nDepth;
  48. }
  49. int MaxDistance(const S_TreeNode *psRootNode)
  50. {
  51. int nDistance = 0;
  52. if (NULL != psRootNode)
  53. {
  54. nDistance = MaxDepth(psRootNode->psLeft) + MaxDepth(psRootNode->psRight);
  55. int nLeftDistance = MaxDistance(psRootNode->psLeft);
  56. int nRightDistance= MaxDistance(psRootNode->psRight);
  57. nDistance = (nLeftDistance > nDistance) ? nLeftDistance : nDistance;
  58. nDistance = (nRightDistance > nDistance) ? nRightDistance : nDistance;
  59. }
  60. return nDistance;
  61. }
  62. int _tmain(int argc, _TCHAR* argv[])
  63. {
  64. S_TreeNode *psRoot = NULL;
  65. AddTreeNode(psRoot, 9);
  66. AddTreeNode(psRoot, 6);
  67. AddTreeNode(psRoot, 4);
  68. AddTreeNode(psRoot, 8);
  69. AddTreeNode(psRoot, 7);
  70. AddTreeNode(psRoot, 15);
  71. AddTreeNode(psRoot, 13);
  72. AddTreeNode(psRoot, 16);
  73. AddTreeNode(psRoot, 18);
  74. cout << "任意两个节点间的最大距离为:" << MaxDistance(psRoot) << endl;
  75. return 0;
  76. }

/*-----------------------------
Copyright by yuucyf. 2011.09.02
------------------------------*/
#include "stdafx.h"
#include 
#include 
using namespace std;

typedef struct tagSBTreeNode
{
	tagSBTreeNode *psLeft;
	tagSBTreeNode *psRight;
	int	nValue;

	int nMaxLeft;
	int nMaxRight;

	tagSBTreeNode()
	{
		psLeft = psRight = NULL;
		nValue = 0;
		nMaxLeft = nMaxRight = 0;
	}
}S_TreeNode;


void AddTreeNode(S_TreeNode *&psTreeNode, int nValue)
{
	if (NULL == psTreeNode)
	{
		psTreeNode = new S_TreeNode;
		assert(NULL != psTreeNode);
		psTreeNode->nValue = nValue;
	}
	else if (psTreeNode->nValue < nValue)
	{
		AddTreeNode(psTreeNode->psRight, nValue);
	}
	else
		AddTreeNode(psTreeNode->psLeft, nValue);
}

int MaxDepth(const S_TreeNode *psTreeNode)
{
	int nDepth = 0;
	if (NULL != psTreeNode)
	{
		int nLeftDepth = MaxDepth(psTreeNode->psLeft);
		int nRightDepth = MaxDepth(psTreeNode->psRight);
		nDepth = (nLeftDepth > nRightDepth) ? nLeftDepth : nRightDepth;
		nDepth++;
	}

	return nDepth;
}

int MaxDistance(const S_TreeNode *psRootNode)
{
	int nDistance = 0;
	if (NULL != psRootNode)
	{
		nDistance = MaxDepth(psRootNode->psLeft) + MaxDepth(psRootNode->psRight);
		int nLeftDistance = MaxDistance(psRootNode->psLeft);
		int nRightDistance= MaxDistance(psRootNode->psRight);
		
		nDistance = (nLeftDistance > nDistance) ? nLeftDistance : nDistance;
		nDistance = (nRightDistance > nDistance) ? nRightDistance : nDistance;
	}
	
	return nDistance;
}




int _tmain(int argc, _TCHAR* argv[])
{
	S_TreeNode *psRoot = NULL;
	AddTreeNode(psRoot, 9);
	AddTreeNode(psRoot, 6);
	AddTreeNode(psRoot, 4);
	AddTreeNode(psRoot, 8);
	AddTreeNode(psRoot, 7);
	AddTreeNode(psRoot, 15);
	AddTreeNode(psRoot, 13);
	AddTreeNode(psRoot, 16);
	AddTreeNode(psRoot, 18);

	cout << "任意两个节点间的最大距离为:" << MaxDistance(psRoot) << endl;

	return 0;
}


思路二:

思路一不是效率最高的,因为在计算二叉树的深度的时候存在重复计算。但应该是可读性比较好的,同时也没有改变原有二叉树的结构和使用额外的全局变量。这里之间给出代码,因为代码的注释已经写的非常详细了。

代码如下:

[cpp] view plaincopyprint?

  1. int g_nMaxLeft = 0;
  2. void MaxDistance_2(S_TreeNode *psRoot)
  3. {
  4. // 遍历到叶子节点,返回
  5. if (NULL == psRoot)
  6. return;
  7. // 如果左子树为空,那么该节点的左边最长距离为0
  8. if (psRoot->psLeft == NULL)
  9. {
  10. psRoot->nMaxLeft = 0;
  11. }
  12. // 如果右子树为空,那么该节点的右边最长距离为0
  13. if (psRoot->psRight == NULL)
  14. {
  15. psRoot -> nMaxRight = 0;
  16. }
  17. // 如果左子树不为空,递归寻找左子树最长距离
  18. if (psRoot->psLeft != NULL)
  19. {
  20. MaxDistance_2(psRoot->psLeft);
  21. }
  22. // 如果右子树不为空,递归寻找右子树最长距离
  23. if (psRoot->psRight != NULL)
  24. {
  25. MaxDistance_2(psRoot->psRight);
  26. }
  27. // 计算左子树最长节点距离
  28. if (psRoot->psLeft != NULL)
  29. {
  30. int nTempMax = 0;
  31. if (psRoot->psLeft->nMaxLeft > psRoot->psLeft->nMaxRight)
  32. {
  33. nTempMax = psRoot->psLeft->nMaxLeft;
  34. }
  35. else
  36. {
  37. nTempMax = psRoot->psLeft->nMaxRight;
  38. }
  39. psRoot->nMaxLeft = nTempMax + 1;
  40. }
  41. // 计算右子树最长节点距离
  42. if (psRoot->psRight != NULL)
  43. {
  44. int nTempMax = 0;
  45. if(psRoot->psRight->nMaxLeft > psRoot->psRight->nMaxRight)
  46. {
  47. nTempMax = psRoot->psRight->nMaxLeft;
  48. }
  49. else
  50. {
  51. nTempMax = psRoot->psRight->nMaxRight;
  52. }
  53. psRoot->nMaxRight = nTempMax + 1;
  54. }
  55. // 更新最长距离
  56. if (psRoot->nMaxLeft + psRoot->nMaxRight > g_nMaxLeft)
  57. {
  58. g_nMaxLeft = psRoot->nMaxLeft + psRoot->nMaxRight;
  59. }
  60. }

int g_nMaxLeft = 0;
void MaxDistance_2(S_TreeNode *psRoot)
{
	// 遍历到叶子节点,返回
	if (NULL == psRoot)
		return;

	// 如果左子树为空,那么该节点的左边最长距离为0
	if (psRoot->psLeft == NULL)
	{
		psRoot->nMaxLeft = 0;
	}

	// 如果右子树为空,那么该节点的右边最长距离为0
	if (psRoot->psRight == NULL)
	{
		psRoot -> nMaxRight = 0;
	}

	// 如果左子树不为空,递归寻找左子树最长距离
	if (psRoot->psLeft != NULL)
	{
		MaxDistance_2(psRoot->psLeft);
	}

	// 如果右子树不为空,递归寻找右子树最长距离
	if (psRoot->psRight != NULL)
	{
		MaxDistance_2(psRoot->psRight);
	}

	// 计算左子树最长节点距离
	if (psRoot->psLeft != NULL)
	{
		int nTempMax = 0;
		if (psRoot->psLeft->nMaxLeft > psRoot->psLeft->nMaxRight)
		{
			nTempMax = psRoot->psLeft->nMaxLeft;
		}
		else
		{
			nTempMax = psRoot->psLeft->nMaxRight;
		}
		psRoot->nMaxLeft = nTempMax + 1;
	}

	// 计算右子树最长节点距离
	if (psRoot->psRight != NULL)
	{
		int nTempMax = 0;
		if(psRoot->psRight->nMaxLeft > psRoot->psRight->nMaxRight)
		{
			nTempMax = psRoot->psRight->nMaxLeft;
		}
		else
		{
			nTempMax = psRoot->psRight->nMaxRight;
		}
		psRoot->nMaxRight = nTempMax + 1;
	}

	// 更新最长距离
	if (psRoot->nMaxLeft + psRoot->nMaxRight > g_nMaxLeft)
	{
		g_nMaxLeft = psRoot->nMaxLeft + psRoot->nMaxRight;
	}
}

人气教程排行