Python solution without defaultdict


  • 0
    M
        queue = [(root, 0)]
        cols = {}
    
        while queue:
            node, i = queue.pop(0)
            if node:
                if i not in cols:
                    cols[i] = []
                cols[i].append(node.val)
                queue += (node.left, i - 1), (node.right, i + 1)
        return [cols[i] for i in sorted(cols)]

Log in to reply
 

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