Accepted Python iterative solution.


  • 0
    X
    class Solution:
        # @param root, a tree node
        # @return a list of lists of integers
        def levelOrder(self, root):
            result = []
            if not root:
                return result
            curlevel = [root]
            while curlevel != []:
                vals = []
                newlevel = []
                for node in curlevel:
                    vals.append(node.val)
                    if node.left:
                        newlevel.append(node.left)
                    if node.right:
                        newlevel.append(node.right)
                result.append(vals)
                curlevel = newlevel
            return result

Log in to reply
 

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