Recursive Python solution without reversing.


  • 2
    X
    class Solution:
        # @param root, a tree node
        # @return a list of lists of integers
        def levelOrderBottom(self, root):
            #not allowed to reverse the list
            result = []
            if root == None:
                return result
            def bfsbottomup(result, prev):
                if prev == []:
                    return
                temp = []
                cur = []
                for node in prev:
                    temp.append(node.val)
                    if node.left != None:
                        cur.append(node.left)
                    if node.right != None:
                        cur.append(node.right)
                bfsbottomup(result, cur)
                result.append(temp)
            bfsbottomup(result, [root])
            return result

Log in to reply
 

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