Python code, no recursive, level order traversal


  • 0
    W
    class Solution(object):
        def maxDepth(self, root):
            if not root:
                return 0
            q,height,max_height=[],1,1
            q.append((root,1))
            while q:
                node,height=q.pop(0)
                max_height=max(max_height, height)
                if node.left:
                    q.append((node.left,height+1))
                if node.right:
                    q.append((node.right,height+1))
            max_height=max(max_height, height)
            return max_height

Log in to reply
 

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