分享

算法5(求二叉树中节点的最大距离)

 白雪~~~ 2012-03-10

题目:求二叉树中节点的最大距离.
如果我们把二叉树看成一个图,父子节点之间的连线看成是双向的,我们姑且定义"距离"为两节点之间边的个数。
写一个程序,求一棵二叉树中相距最远的两个节点之间的距离。

题目来源于:http://topic.csdn.net/u/20101011/16/2befbfd9-f3e4-41c5-bb31-814e9615832e.html

思路一:

题目要求一颗二叉树中两个节点之间的最大距离,由于父子节点之间边是双向的,所以这道题转换为求左子树的最大深度 + 右子树的最大深度就是二叉树中节点的最大距离。

例如:以下一颗树:

5

/ /

4 8

/ /

7 12

/

10

那么它的最大距离为4,即:4-->5-->8-->12-->10.其中"-->"的个数就是最大的距离数4.

代码如下:

  1. /*--------------------------------
  2. 求二叉树的最大距离算法.
  3. Copyright by yuucyf. 2011.05.06
  4. ----------------------------------*/
  5. #include "stdafx.h"
  6. #include <assert.h>
  7. #include <iostream>
  8. using namespace std;
  9. typedef struct tagBSTNode
  10. {
  11. int nValue;
  12. tagBSTNode *psLeft;
  13. tagBSTNode *psRight;
  14. tagBSTNode()
  15. {
  16. nValue = 0;
  17. psLeft = psRight = NULL;
  18. }
  19. }S_BSTNode;
  20. void DeleteAllNode(S_BSTNode* pRoot)
  21. {
  22. if (NULL == pRoot) return;
  23. DeleteAllNode(pRoot->psLeft);
  24. DeleteAllNode(pRoot->psRight);
  25. delete pRoot;
  26. }
  27. void AddBSTreeNode(S_BSTNode* &pRoot, int nValue)
  28. {
  29. if (NULL == pRoot)
  30. {
  31. pRoot = new S_BSTNode;
  32. assert(NULL != pRoot);
  33. pRoot->nValue = nValue;
  34. return;
  35. }
  36. if (pRoot->nValue > nValue)
  37. {
  38. AddBSTreeNode(pRoot->psLeft, nValue);
  39. }
  40. else if ( pRoot->nValue < nValue)
  41. {
  42. AddBSTreeNode(pRoot->psRight, nValue);
  43. }
  44. }
  45. int GetMaxDepth(const S_BSTNode* pRoot)
  46. {
  47. if (NULL == pRoot) return 0;
  48. if (NULL == pRoot->psLeft && NULL == pRoot->psRight)
  49. {
  50. return 0;
  51. }
  52. int nLeftDepth = 0, nRightDepth = 0;
  53. if (NULL != pRoot->psLeft)
  54. {
  55. nLeftDepth = GetMaxDepth(pRoot->psLeft) + 1;
  56. }
  57. if (NULL != pRoot->psRight)
  58. {
  59. nRightDepth = GetMaxDepth(pRoot->psRight) + 1;
  60. }
  61. if (nRightDepth >= nLeftDepth)
  62. {
  63. return nRightDepth;
  64. }
  65. else
  66. {
  67. return nLeftDepth;
  68. }
  69. }
  70. int GetMaxDistance(const S_BSTNode* pRoot)
  71. {
  72. if (NULL == pRoot) return 0;
  73. int nLeftDepth = GetMaxDepth(pRoot->psLeft);
  74. nLeftDepth += ((pRoot->psLeft != NULL) ? 1 : 0);
  75. int nRightDepth =GetMaxDepth(pRoot->psRight);
  76. nRightDepth += ((pRoot->psRight != NULL) ? 1 : 0);
  77. return nLeftDepth + nRightDepth;
  78. }
  79. int _tmain(int argc, _TCHAR* argv[])
  80. {
  81. S_BSTNode *pRoot = NULL;
  82. AddBSTreeNode(pRoot, 5);
  83. AddBSTreeNode(pRoot, 4);
  84. AddBSTreeNode(pRoot, 8);
  85. AddBSTreeNode(pRoot, 7);
  86. AddBSTreeNode(pRoot, 12);
  87. AddBSTreeNode(pRoot, 10);
  88. int nMaxDis = GetMaxDistance(pRoot);
  89. cout << "Max distance is " << nMaxDis << endl;
  90. DeleteAllNode(pRoot);
  91. return 0;
  92. }

思路二:

其实想法跟思路一是类似的,具体实现看以下代码,不过你会发现下面的实现没有比思路一中的实现更为容易理解。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多