Accepted Answer. But had to externalize the method


  • 1
    L
    def depth(root):
        if root == None:
            return 0
        return max(depth(root.left), depth(root.right)) + 1
    
    class Solution:
        # @param root, a tree node
        # @return an integer
        def maxDepth(self, root):
            return depth(root)
    

    Really simple way of solving the question, through recursion.


  • 1
    K

    how about

    class Solution:
        # @param root, a tree node
        # @return an integer
        def maxDepth(self, root):
            if not root:
                return 0
    
            return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1

Log in to reply
 

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