LeetCode543. Diameter of Binary Tree

Solution1

If we think about it, there may be three conditions for the largest diameter of a binary tree.

  1. The largest diamater exists in the left subtree of the root.
  2. The largest diameter exits in the right subtree of the root.
  3. The largest diameter exists and contains the root, that is, the corresponding path of the largest diameter extends from left subtree, including root, to right subtree.

And considering the property of a binary tree, these conditions can be applied to every node in the tree. Thus we can write a helper function to deal with this question. For every node, we will return 3 values to its parent: left subtree's max depth, right subtree's max length, and max diameter of current tree rooted at this node.

For each tree node, max left depth, max right depth, max diameter could be calculated as following respectively:

  1. max_left_depth = max(left_subtree's max_left depth, left subtree's max_right_depth) + 1
  2. max_right_depth = max(right_subtree's max_left depth, right subtree's max_right_depth) + 1
  3. max_diameter = max(left_subtree's max_diameter, right_subtree's max_diameter, max_left_subtree_depth + max_right_subtree_depth + 1)

As a result, for the root node, we just need to return the 3rd value we get since this records the maximum diameter for the whole tree. However, since the actual definition of the diameter is the number of edges, we simply deduct 1 from the value we get, which is the number of nodes in the longest diameter.

Suppose there are n nodes in the tree, this algorithm runs in O(logn) time and O(1) space without considering the recursion stack.

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int diameterOfBinaryTree(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int ret[] = diameterHelper(root);
        return ret[2] - 1;
    }
    
    private int[] diameterHelper(TreeNode curr) {
        if (curr == null) return new int[]{0, 0, 0};
        
        // [max depth of left subtree, max depth of right subtree, max diameter of left-curr-right subtree]
        int[] ret = new int[3];
        int[] leftRet = diameterHelper(curr.left);
        int[] rightRet = diameterHelper(curr.right);
        ret[0] = Math.max(leftRet[0], leftRet[1]) + 1;
        ret[1] = Math.max(rightRet[0], rightRet[1]) + 1;
        int maxDiameter = Math.max(leftRet[0], leftRet[1]) + Math.max(rightRet[0], rightRet[1]) + 1;
        maxDiameter = Math.max(maxDiameter, leftRet[2]);
        maxDiameter = Math.max(maxDiameter, rightRet[2]);
        ret[2] = maxDiameter;
        
        return ret;
    }
}

Solution2

However, from the above solution, it can be seen that the actual longest diameter for every node is just the max_left_depth + max_right_depth. Hence we could use a global max value to keep track of the largest diameter to make the code clean.

class Solution {
    int max = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        diameterHelper(root);
        return max;
    }
    
    private int diameterHelper(TreeNode root) {
        if (root == null) return 0;
        
        int leftDep = diameterHelper(root.left);
        int rightDep = diameterHelper(root.right);
        max = Math.max(max, leftDep + rightDep);
        return Math.max(leftDep, rightDep) + 1;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,769评论 0 33
  • 姥姥,你还好吗?昨天夜里,我又梦到你了,梦到你和姥爷在天堂里坐在一起晒着太阳,梦到你和姥爷乐呵呵地笑着,我知道,你...
    梅花映雪阅读 445评论 9 6
  • 最近身边很多人都说:“杨紫越变越美了。”是啊,《家有儿女》里的童星都长大了,女大十八变,她早已经不是我们记忆中的小...
    潮流一起说阅读 446评论 0 0
  • 圣诞节就要到了,我又想起了红鼻子鹿鲁道夫的故事。鲁道夫因为它的红鼻子而被人嘲笑,也因为它的红鼻子成为了圣诞老...
    吴恩泽阅读 738评论 0 0
  • 感觉最近的雨就像憋坏的膀胱,怎么尿都尿不尽。在这样一个湿嗒嗒的季节,小编开始蠢蠢欲动起来。这次我们玩把大的。果照晒...
    爱叮叮阅读 512评论 0 1