Easy to understand python solution - dfs


  • 0
    R
    def levelOrderBottom(self, root):
        if root is None:
            return []
        res = []
        self.dfs([root], res)
        return res
        
    def dfs(self, level_nodes, res):
        if not level_nodes: return
        res.insert(0, [])
        temp = []
        for node in level_nodes:
            res[0].append(node.val)
            if node.left: temp.append(node.left)
            if node.right: temp.append(node.right)
        self.dfs(temp, res)
    

    idea is the same as the first problem. no need to mark each level.


Log in to reply
 

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