Easy Python solution with recursion

  • 0
    class Solution(object):
        def depthOfBinaryTree(self, root):
            d, l, r = 0, 0, 0
            if root is None: return 0
            if root.left: l = (self.depthOfBinaryTree(root.left)+1)
            if root.right: r = (self.depthOfBinaryTree(root.right)+1)
            return max(l, r)
        def diameterOfBinaryTree(self, root):
            if root is None: return 0
            d = 0
            if root.left: d += (self.depthOfBinaryTree(root.left)+1)
            if root.right: d += (self.depthOfBinaryTree(root.right)+1)
            return max(self.diameterOfBinaryTree(root.left), self.diameterOfBinaryTree(root.right), d)

Log in to reply

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