5-line rec python


  • 1
    R
    class Solution(object):
        def levelOrder(self, root):
            r = []
            def rec(root, k):
                if root:
                    if len(r) < k+1:
                        r.append([])
                    r[k].append(root.val)
                    rec(root.left, k+1)
                    rec(root.right, k+1)
                    
            rec(root, 0)
            return r

Log in to reply
 

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