Simple Python recursion with comment


  • 0

    Null root returns 0, otherwise increment depth: 1+ maxDepth(subtree)

    class Solution(object):

    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        
        #base condition
        if not root:
            return 0
        
        #recusion
        return 1+max(self.maxDepth(root.left), self.maxDepth(root.right))

Log in to reply
 

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