Simple Python Solution, Using Stack


  • 0
    L

    here is my solution:
    '''

    def maxDepth(self,root):
        if not root:
            return 0
        stack = []
        maximum = 1
        stack.append((root,1))
        while stack:
            root,d = stack.pop()
            if root.left:
                stack.append((root.left,d+1))
            if root.right:
                stack.append((root.right,d+1))
            if not root.left and not root.right :
                maximum = max(maximum,d)
                
        return maximum
    

    '''


Log in to reply
 

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