c++实现二叉树中节点的最大距离(二)

2014-11-24 08:28:28 · 作者: · 浏览: 1
)i; } for(i=0;i<=2;i++) { tree[i]->pLeft=tree[2*i+1]; tree[i]->pRight=tree[2*i+2]; } tree[3]->pLeft=tree[7]; tree[5]->pRight=tree[8]; return tree[0]; } int main() { FindMaxLen(InitTree()); printf("%d\n",nMaxLen); return 0; } \
\


5、很明显,nMaxLeft和nMaxRight在完成我们DP的记账本同时,破坏了二叉树的结构,这将给后者的应用性带来非常大的负担,逻辑上相对复杂,Milo给出了更为简洁的,DP版本。

[cpp] view plaincopyprint 在CODE上查看代码片派生到我的代码片
  1. #include
  2. using namespace std;
  3. struct NODE {
  4. NODE *pLeft; NODE *pRight;
  5. };
  6. struct RESULT {
  7. int nMaxDistance; int nMaxDepth;
  8. };
  9. RESULT GetMaximumDistance(NODE* root) {
  10. if (!root) {
  11. RESULT empty = { 0, -1 }; // trick: nMaxDepth is -1 and then caller will plus 1 to balance it as zero. return empty;
  12. }
  13. RESULT lhs = GetMaximumDistance(root->pLeft); RESULT rhs = GetMaximumDistance(root->pRight);
  14. RESULT result;
  15. result.nMaxDepth = max(lhs.nMaxDepth + 1, rhs.nMaxDepth + 1); result.nMaxDistance = max(max(lhs.nMaxDistance, rhs.nMaxDistance), lhs.nMaxDepth + rhs.nMaxDepth + 2);
  16. return result; }
    #include 
          
           
     
    using namespace std;
     
    struct NODE
    {
        NODE *pLeft;
        NODE *pRight;
    };
     
    struct RESULT
    {
        int nMaxDistance;
        int nMaxDepth;
    };
     
    RESULT GetMaximumDistance(NODE* root)
    {
        if (!root)
        {
            RESULT empty = { 0, -1 };   // trick: nMaxDepth is -1 and then caller will plus 1 to balance it as zero.
            return empty;
        }
     
        RESULT lhs = GetMaximumDistance(root->pLeft);
        RESULT rhs = GetMaximumDistance(root->pRight);
     
        RESULT result;
        result.nMaxDepth = max(lhs.nMaxDepth + 1, rhs.nMaxDepth + 1);
        result.nMaxDistance = max(max(lhs.nMaxDistance, rhs.nMaxDistance), lhs.nMaxDepth + rhs.nMaxDepth + 2);
        return result;
    }
          

    这里借用Milo的自荐:

    计算 result 的代码很清楚;nMaxDepth 就是左子树和右子树的深度加1;nMaxDistance 则取 A 和 B 情况的最大值。

    为了减少 NULL 的条件测试,进入函数时,如果节点为 NULL,会传回一个 empty 变量。比较奇怪的是 empty.nMaxDepth = -1,目的是让调用方 +1 后,把当前的不存在的 (NULL) 子树当成最大深度为 0。

    除了提高了可读性,这个解法的另一个优点是减少了 O(节点数目) 大小的侵入式资料,而改为使用 O(树的最大深度) 大小的栈空间。这个设计使函数完全没有副作用(side effect)。

    测试代码:

    [cpp] view plaincopyprint 在CODE上查看代码片派生到我的代码片
    1. void Link(NODE* nodes, int parent, int left, int right) {
    2. if (left != -1) nodes[parent].pLeft = &nodes[left];
    3. if (right != -1)
    4. nodes[parent].pRight = &nodes[right]; }
    5. void main()
    6. { // P. 241 Graph 3-12
    7. NODE test1[9] = { 0 }; Link(test1, 0, 1, 2);
    8. Link(test1, 1, 3, 4); Link(test1, 2, 5, 6);
    9. Link(test1, 3, 7, -1); Link(test1, 5, -1, 8);
    10. cout << "test1: " << GetMaximumDistance(&test1[0]).nMaxDistance << endl;
    11. // P. 242 Graph 3-13 left NODE test2[4] = { 0 };
    12. Link(test2, 0, 1, 2); Link(test2, 1, 3, -1);
    13. cout << "test2: " << GetMaximumDistance(&test2[0]).nMaxDistance << endl;
    14. // P. 242 Graph 3-13 right NODE test3[9] = { 0 };
    15. Link(test3, 0, -1, 1); Link(test3, 1, 2, 3);
    16. Link(test3, 2, 4, -1); Link(test3, 3, 5, 6);
    17. Link(test3, 4, 7, -1); Link(test3, 5, -1, 8);
    18. cout << "test3: " << GetMaximumDistance(&test3[0]).nMaxDistance << endl;
    19. // P. 242 Graph 3-14 // Same as Graph 3-2, not test
    20. // P. 243 Graph 3-15
    21. NODE test4[9] = { 0 }; Link(test4, 0, 1, 2);
    22. Link(test4, 1, 3, 4); Link(test4, 3, 5, 6);
    23. Link(test4, 5, 7, -1); Link(test4, 6, -1, 8);
    24. cout << "test4: " << GetMaximumDistance(&test4[0]).nMaxDistance << endl; }
      void Link(NODE* nodes, int parent, int left, int right)
      {
          if (left != -1)
              nodes[parent].pLeft = &nodes[left]; 
       
          if (right != -1)
              nodes[parent].pRight = &nodes[right];
      }
       
      void main()
      {
          // P. 241 Graph 3-12
          NODE test1[9] = { 0 };
          Link(test1, 0, 1, 2);
          Link(test1, 1, 3, 4);
          Link(test1, 2, 5, 6);
          Link(test1, 3, 7, -1);
          Link(test1, 5, -1, 8);
          cout << "test1: " << GetMaximumDistance(&test1[0]).nMaxDistance << endl;
       
          // P. 242 Graph 3-13 left
          NODE test2[4] = { 0 };
          Link(test2, 0, 1, 2);
          Link(test2, 1, 3, -1);
          cout << "test2: " << GetMaximumDistance(&test2[0]).nMaxDistance << endl;
       
          // P. 242 Graph 3-13 right
          NODE test3[9] = { 0 };
          Link(test3, 0, -1, 1);
          Link(test3, 1, 2, 3);
          Link(test3, 2, 4, -1);
          Link(test3, 3, 5, 6);
          Link(test3, 4, 7, -1);
          Link(test3, 5, -1, 8);
          cout << "test3: " << GetMaximumDistance(&test3[0]).nMaxDistance << endl;
       
          // P. 242 Graph 3-14
          // Same as Graph 3-2, not test
       
          // P. 243 Graph 3-15
          NODE test4[9] = { 0 };
          Link(test4, 0, 1, 2);
          Link(test4, 1, 3, 4);
          Link(test4, 3, 5, 6);
          Link(test4, 5, 7, -1);
          Link(test4, 6, -1, 8);
          cout << "test4: " << GetMaximumDistance(&test4[0]).nMaxDistance << endl;
      }

      \

      \