Using a queue in Python

  • 0
    # 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 levelOrderBottom(self, root):
            :type root: TreeNode
            :rtype: List[List[int]]
            results = []
            if not root : 
                return results
            queue = deque([root])
            while queue:
                level = []
                for i in range(len(queue)):
                    front = queue.popleft()
                    if front.left:
                    if front.right:
            return results[::-1]

Log in to reply

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