计算二叉树中节点的最大距离

主要有两种思路。

思路一:

分情况讨论,

  • 情况A: 路径经过左子树的最深节点,通过根节点,再到右子树的最深节点。
  • 情况B: 路径不穿过根节点,而是左子树或右子树的最大距离路径,取其大者。

计算这两个情况的路径距离,并取其大者,就是该二叉树的最大距离。

struct Node
{
    struct Node * leftChild;
    struct Node * rightChild;
};
struct Result
{
    int nMaxDistance;
    int nMaxDepth;
};

Result GetMaximumDistance(Node* root)
{
    if (!root)
    {
        Result empty = { 0, -1 };
        return empty;
    }
    Result lhs = GetMaximumDistance(root->leftChild);
    Result rhs = GetMaximumDistance(root->rightChild);
    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;
}


思路二:

计算每个节点的左子树和右子树的高度和,取最大的。

这是原始的解法。

// 数据结构定义
struct NODE
{
    NODE* pLeft;        // 左子树
    NODE* pRight;       // 右子树
    int nMaxLeft;       
    int nMaxRight;      
    char chValue;
};
 
int nMaxLen = 0;
 
void FindMaxLen(NODE* pRoot)
{
    // 遍历到叶子节点,返回
    if(pRoot == NULL)
    {
        return;
    }
 
    if(pRoot -> pLeft == NULL)
    {
        pRoot -> nMaxLeft = 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;
    }
}


这是第二种解法。

struct Node{
  Node *left;
  Node *right;
};

int tree_height(Node *root, int &max_d)
{
  //每碰到一个子节点,高度自增1,可以设空节点高度为-1
  if (root==NULL) return -1;
  int a = tree_height(root->left, max_d) + 1;   
  int b = tree_height(root->right, max_d) + 1; 
  int c = a+b;
  if (max_d < c) max_d = c;
  return  a>b ? a : b;
}

int maxDis(Node* root)
{
  int max_d=0;
  tree_height(root, max_d);
  return max_d;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要创建二叉树,可以使用结构体来定义二叉树结点,结构体包含指向左右子树的指针,如下所示: ``` struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; ``` 接着可以使用递归的方法创建二叉树,每次读入一个结点的值,如果值为0,则表示该结点为空;否则新建一个结点,并递归创建左右子树。具体代码如下: ``` struct TreeNode* createTree() { int val; scanf("%d", &val); if (val == 0) { return NULL; } struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); node->val = val; node->left = createTree(); node->right = createTree(); return node; } ``` 关于任意两个结点的距离,可以转化为它们的最近公共祖先节点,并计算出到该祖先节点距离。可以使用后序遍历的方法解。对于每个节点,先递归处理其左右子树,得到它们的最近公共祖先节点距离,然后根据当前节点的左右子树情况计算出当前节点的最近公共祖先节点距离,并返回给上一级调用。具体代码如下: ``` struct Result { struct TreeNode* ancestor; // 最近公共祖先 int distance; // 距离 }; struct Result postorder(struct TreeNode* root, int p, int q) { if (root == NULL) { return (struct Result){NULL, -1}; } struct Result left = postorder(root->left, p, q); struct Result right = postorder(root->right, p, q); if (left.ancestor != NULL && left.ancestor != NULL) { // p和q分别在左右子树 return (struct Result){root, left.distance + right.distance + 2}; } else if (root->val == p || root->val == q) { // 当前节点是p或q的一个 if (left.ancestor != NULL || right.ancestor != NULL) { // 另一个在子树 return (struct Result){root, left.distance + right.distance + 2}; } else { // 另一个不在子树 return (struct Result){root, 0}; } } else { // p和q都在同一子树 if (left.ancestor != NULL) { return (struct Result){left.ancestor, left.distance + 1}; } else { return (struct Result){right.ancestor, right.distance + 1}; } } } int distance(struct TreeNode* root, int p, int q) { struct Result result = postorder(root, p, q); return result.distance; } ``` 注意,在使用完二叉树后需要释放内存,可以使用递归的方法进行释放,具体代码如下: ``` void freeTree(struct TreeNode* root) { if (root == NULL) { return; } freeTree(root->left); freeTree(root->right); free(root); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值