My Solution using one Queue and delimiter in queue


  • 0
    R
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def levelOrderBottom(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            if root == None:
                return []
            queue = []
            queue.append(root)
            queue.append(None)
            result = []
            m = []
            while queue:
                global m
                item = queue.pop(0)
                if item is not None:
                    if item.left:
                        queue.append(item.left)
                    if item.right:
                        queue.append(item.right)
                if len(queue) == 0:
                    result.append(m)
                    break
                if item == None:
                    queue.append(None)
                    result.append(m)
                    m = []
                elif item is not None:
                    m.append(item.val)
            return result[::-1]
    

Log in to reply
 

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