Simple BFS in Python


  • 0
    K
        def connect(self, root):
    
            queue = []
            depth = -1
            if root:
                queue.append([root, depth+1])
    
            while queue:
                node, d = queue.pop(0)
                if d - depth == 1:
                    prev = node
                else:
                    node.next = prev
                    prev = node
                depth = d
    
                for child in [node.right, node.left]:
                    if child:
                        queue.append([child, d+1])
    

Log in to reply
 

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