二叉树的直径(左右子树的深度和)Diameter of Binary Tree

题目:

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

Example:
Given a binary tree 

          1
         / \
        2   3
       / \     
      4   5

Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].

Note: The length of path between two nodes is represented by the number of edges between them.

解决:

①  题目要求求出二叉树的直径:二叉树中从一个结点到另一个节点最长的路径,叫做二叉树的直径

我们可以理解为求根节点的左右子树的深度和,那么我们只要对每一个结点求出其左右子树深度之和,就可以更新结果diameter了。为了减少重复计算,我们用hashmap建立每个结点和其深度之间的映射,这样某个结点的深度之前计算过了,就不用再次计算了。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

public class Solution {//321 ms
    public int diameterOfBinaryTree(TreeNode root) {
        if(root == null) return 0;
        int diameter = getDepth(root.left) + getDepth(root.right);
        return Math.max(diameter,
                                 Math.max(diameterOfBinaryTree(root.left),diameterOfBinaryTree(root.right)));//不只是头节点

    }
    public int getDepth(TreeNode node){
        Map<TreeNode,Integer> map = new HashMap<>();
        if(node == null) return 0;
        if(map.containsKey(node) && map.get(node) > 0) return map.get(node);
        int depth = Math.max(getDepth(node.left),getDepth(node.right)) + 1;
        map.put(node,depth);
        return map.get(node);
    }
}

② 思路和上一个相同,但是要简洁的多,没有使用map,只使用一个递归即可。在求深度的递归函数中顺便就把直径算出来了。

public class Solution {//10ms
    int diameter = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        getDepth(root);
        return diameter;
    } 
    private int getDepth(TreeNode root) {
        if(root == null) return 0;
        int left = getDepth(root.left);
        int right = getDepth(root.right);
        diameter = Math.max(diameter, left + right); //不是left + right + 1。。是path。。不是number of node
        return Math.max(left, right) + 1; 
    }
}

③ 进化版。

public class Solution {//9ms
    int depth = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        getDepth(root);
        return depth;
    }
    public int getDepth(TreeNode root) {
        if (root == null) return 0;
        int left = getDepth(root.left);
        int right = getDepth(root.right);
        if (depth < left + right) {
            depth = left + right;
        }
        return left > right ? left + 1 : right + 1;
    }
}

④ 比较好理解的

class Solution {//23ms
    public int diameterOfBinaryTree(TreeNode root) {
        if(root == null) return 0;
        int diameter = getDepth(root.left) + getDepth(root.right);
        return Math.max(diameter,Math.max(diameterOfBinaryTree(root.left),diameterOfBinaryTree(root.right)));
    }
    public int getDepth(TreeNode root){
        if(root == null) return 0;
        return Math.max(getDepth(root.left),getDepth(root.right)) + 1;
    }
}

转载于:https://my.oschina.net/liyurong/blog/1503784

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值