Actually simple python solution


  • 2
    S
    class Solution(object):
        def levelOrder(self, root):
            result = []
            self.level_dfs(root, 0, result)
            return result
            
        def level_dfs(self, node, level, result):
            if not node:
                return
            if level == len(result):
                result.append([])
            result[level].append(node.val)
            self.level_dfs(node.left, level+1, result)
            self.level_dfs(node.right, level+1, result)

  • 0
    N

    You could have the result[] as a class variable and then you don't have to pass a pointer around


Log in to reply
 

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