二叉树最大距离(直径)

方法一 计算每个节点的左子树和右子树的高度和,加上根本身(边数为2),取最大值

二叉树的最大距离,一定是经过根或者某个子树的根的路径,其两个端点必然是叶子节点或者根节点。计算二叉树的高度,并且比较经过该节点的最大距离,取

最大者。其中,getTreeDistance的起点是-1,其值为二叉树的高度减1,即高度路径上的点之间的边的条数,即距离。getTreeDistance引入了新的概念描述,

getTreeHeight是求树的高度,但是其计算时候要减去1.经过一个根节点的最大距离,就是其左子树最大高度距离(不是最大距离),加上其右子树最大高度距离,加上

左右到根节点的距离,即2,然后与一个外部变量比较,因此使用了两个函数。

实现代码如下:

function Node(val){
    this.val = val;
    this.left = null;
    this.right = null;
}
function getMaxDistance(){ var max_d = 0; getTreeHeight(root);//或者getTreeDistance(root); return max_d; }
function getTreeDistance(root){ if(!root){ return -1; } var lh = getTreeDistance(root.left), rh = getTreeDistance(root.right); var distance = lh + rh + 2; max_d = Math.max(distance,max_d); return Math.max(lh,rh) + 1; }
function getTreeHeight(root){ if(!root){ return 0; } var lh = getTreeHeight(root.left), rh = getTreeHeight(root.right); var distance = (lh -1) + (rh - 1) + 2; max_d = Math.max(distance,max_d); return Math.max(lh,rh) + 1; }
方法二 按照节点是否跨根,分为左子树最大距离,右子树最大距离,左右子树最大深度加到根节点的距离,即2,取最大者
同样区分高度距离和高度,起点不同,高度距离为-1,高度为0,用对象距离结果,然后返回。
function getMaxDistance(root){
    if(!root){
        return {
            maxDist:-1,                    
            maxDistance:0
        }
    }
    var lRes = getMaxDistance(root.left),
        rRes = getMaxDistance(root.right);
    var result = {
        maxDist:Math.max(lRes.maxDist,rRes.maxDist) + 1,
        maxDistance:Math.max(lRes.maxDistance,rRes.maxDistance,lRes.maxDist + rRes.maxDist + 2)
    }
    return result;
}

function getMaxDistance(root){
    if(!root){
        return {
            maxDepth:0,                    
            maxDistance:0
        }
    }
    var lRes = getMaxDistance(root.left),
        rRes = getMaxDistance(root.right);
    var result = {
        maxDepth:Math.max(lRes.maxDepth,rRes.maxDepth) + 1,
        maxDistance:Math.max(lRes.maxDistance,rRes.maxDistance,(lRes.maxDepth-1) + (rRes.maxDepth-1) + 2)
    }
    return result;
}

 

出处: http://blog.csdn.net/flyinghearts/article/details/5605995

    http://www.cnblogs.com/miloyip/archive/2010/02/25/1673114.html    

转载于:https://www.cnblogs.com/mengff/p/6870011.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉树的最长直径是指二叉树中任意两个节点之间最长路径长度,也就是最长的一条路径。可以使用深度优先搜索算法(DFS)来求解。 具体做法如下: 1. 对于每个节点x,求出其左子树的深度L和右子树的深度R; 2. 计算以节点x为根节点的子树的最长直径为L+R; 3. 对于每个节点,比较其左子树的最长直径、右子树的最长直径和以该节点为根节点的子树的最长直径,取最大值即为二叉树的最长直径。 下面是一个 Python 实现的例子: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def __init__(self): self.diameter = 0 def diameterOfBinaryTree(self, root: TreeNode) -> int: self.getDepth(root) return self.diameter def getDepth(self, node: TreeNode) -> int: if not node: return 0 left_depth = self.getDepth(node.left) right_depth = self.getDepth(node.right) self.diameter = max(self.diameter, left_depth + right_depth) return max(left_depth, right_depth) + 1 ``` 在上面的代码中,我们定义了一个 `Solution` 类,并实现了 `diameterOfBinaryTree` 和 `getDepth` 两个方法。其中,`diameterOfBinaryTree` 方法用于求解二叉树的最长直径,而 `getDepth` 方法用于求解二叉树中每个节点的深度。 在 `getDepth` 方法中,我们使用递归的方式求解每个节点的深度,并在递归过程中计算以该节点为根节点的子树的最长直径,并将其与当前最大直径比较取最大值。 最后,我们可以调用 `diameterOfBinaryTree` 方法来求解二叉树的最长直径

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值