Easy to Understand Python Solution


  • 0
    F
    class Solution(object):
        def diameterOfBinaryTree(self, root):
            
            """
            :type root: TreeNode
            :rtype: int
            """
            path = [0]
            def helper(root):
                if not root:
                    return 0
                left_depth, right_depth = 0, 0
                if root.left:
                    left_depth = 1 + helper(root.left)
                if root.right:
                    right_depth = 1 + helper(root.right)
                if path[0] < left_depth + right_depth:
                    path[0] = left_depth + right_depth
                return max(left_depth, right_depth)
            helper(root)
            return path[0]
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.