Simple Python Recursive Solution


  • 0
    K
    def levelOrder(self, root):
            """
            :type root: TreeNode
            :rtype: List[List[int]]
            """
            ans = []
            def recurse(node,level):
                if not node: return
                if len(ans) <= level:
                    ans.append([node.val])
                else:
                    ans[level].append(node.val)
                recurse(node.left,level+1)
                recurse(node.right,level+1)
            recurse(root,0)
            return ans
    

Log in to reply
 

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