Python with reusable level stack


  • 0
    Y
    class Solution(object):
        def levelOrder(self, root):
    
            if root == None:
                return []
            levelstack = [root]
            answer = []
            while len(levelstack) > 0:
                temp = []
                tempval = []
                for node in levelstack:
                    tempval.append(node.val)
                    if node.left:
                        temp.append(node.left)
                    if node.right:
                        temp.append(node.right)
                levelstack = temp
                answer.append(tempval)
            return answer

Log in to reply
 

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