Python solution using global variable


  • 0
    Z
    class Solution(object):
        def diameterOfBinaryTree(self, root):
            self.max_len = 0
            def helper(root):
                if not root: return 0
                l, r = helper(root.left), helper(root.right)
                self.max_len = max(self.max_len, l + r)
                return max(l, r) + 1
            helper(root)
            return self.max_len

Log in to reply
 

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