【学习点滴-数据结构-二叉树】求二叉树两个节点之间的最大距离

来源:互联网 发布:mysql unique select 编辑:程序博客网 时间:2024/06/07 00:24
/* *1.编程之美上的解法。  */struct NODE{     NODE* pLeft;        // 左子树     NODE* pRight;      // 右子树     int nMaxLeft;      // 左子树中的最长距离     int nMaxRight;     // 右子树中的最长距离     char chValue;    // 该节点的值};int nMaxLen = 0;// 寻找树中最长的两段距离void FindMaxLen(NODE* pRoot){     // 遍历到叶子节点,返回     if(pRoot == NULL)     {          return;     }     // 如果左子树为空,那么该节点的左边最长距离为0     if(pRoot -> pLeft == NULL)     {          pRoot -> nMaxLeft = 0;      }     // 如果右子树为空,那么该节点的右边最长距离为0     if(pRoot -> pRight == NULL)     {          pRoot -> nMaxRight = 0;     }     // 如果左子树不为空,递归寻找左子树最长距离     if(pRoot -> pLeft != NULL)     {          FindMaxLen(pRoot -> pLeft);     }     // 如果右子树不为空,递归寻找右子树最长距离     if(pRoot -> pRight != NULL)     {          FindMaxLen(pRoot -> pRight);     }     // 计算左子树最长节点距离     if(pRoot -> pLeft != NULL)     {          int nTempMax = 0;          if(pRoot -> pLeft -> nMaxLeft > pRoot -> pLeft -> nMaxRight)          {               nTempMax = pRoot -> pLeft -> nMaxLeft;          }          else          {               nTempMax = pRoot -> pLeft -> nMaxRight;          }          pRoot -> nMaxLeft = nTempMax + 1;     }     // 计算右子树最长节点距离     if(pRoot -> pRight != NULL)     {          int nTempMax = 0;          if(pRoot -> pRight -> nMaxLeft > pRoot -> pRight -> nMaxRight)          {               nTempMax = pRoot -> pRight -> nMaxLeft;          }          else          {               nTempMax = pRoot -> pRight -> nMaxRight;          }          pRoot -> nMaxRight = nTempMax + 1;     }     // 更新最长距离     if(pRoot -> nMaxLeft + pRoot -> nMaxRight > nMaxLen)     {          nMaxLen = pRoot -> nMaxLeft + pRoot -> nMaxRight;     }}



//其他解法2#include <stdio.h>#include <stdlib.h>#include <malloc.h>#define LEAF -1typedef struct BTreeNode{     BTreeNode* lchild;     BTreeNode* rchild;     int value;}BTreeNode,*Btree;BTreeNode* createTree(){     BTreeNode* T;     int t;     scanf("%d",&t);     if(t == LEAF){          T = NULL;     }else{          T = (BTreeNode *) malloc(sizeof(BTreeNode));          T->value = t;          T->lchild = createTree();          T->rchild = createTree();     }     return T;}int max(int a,int b){return a > b ? a : b;}int findMaxLen(BTreeNode* root,int &depth){    if(root == NULL){depth = 0;return 0;}    int leftLen = 0,rightLen = 0,maxLeft = 0,maxRight = 0;if(root->lchild != NULL){maxLeft = findMaxLen(root->lchild,leftLen);}if(root->rchild != NULL){maxRight = findMaxLen(root->rchild,rightLen);}depth = max(leftLen,rightLen) + 1;        return max(maxLeft,max(maxRight,leftLen + rightLen));}main(){    BTreeNode * root;    root = createTree();int depth = 0,maxLen = 0;    maxLen = findMaxLen(root,depth);    printf("%d \n",maxLen);    system("pause");    return 0;}


//引用:http://www.cnblogs.com/miloyip/archive/2010/02/25/binary_tree_distance.html//解法3using 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;}


原创粉丝点击