Python Solution(Deque) -- 59ms


  • 0
    K
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    from collections import deque
    class Solution(object):
        def maxDepth(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if not root :
                return 0
            que = deque([(root,1)])
            result = 0
            while que:
                t,depth = que.popleft()
                result = max(result,depth) 
                if t.left :
                    que.append((t.left,depth+1))
                if t.right :
                    que.append((t.right,depth+1))
            return result

Log in to reply
 

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