Python solution, 48 ms


  • 0
    F
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def maxDepth(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if root is None:
                return 0
            def calc_depth(tree):
                if tree.left is None and tree.right is None:
                    return 1
                elif tree.left is None:
                    return calc_depth(tree.right) + 1
                elif tree.right is None:
                    return calc_depth(tree.left) + 1
                else:
                    return max([calc_depth(tree.left), calc_depth(tree.right)]) + 1
            return calc_depth(root)```

Log in to reply
 

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