a Python solution


  • 0
    L
    class Solution(object):
        def levelOrder(self, root):
            res = []
            self.BFS(root, res, 0)
            return res
            
        def BFS(self, root, res, level):
            if not root:
                return
            try:
                res[level].append(root.val)
            except:
                res.append([])
                res[level].append(root.val)
            self.BFS(root.left, res, level+1)
            self.BFS(root.right, res, level+1)
    

Log in to reply
 

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