Python easy bfs solution


  • 0
    L
     def connect(self, root):
            if not root: return
            queue=[root]
            while queue:
                level=[]
                for _ in range(len(queue)):
                    node = queue.pop(0)
                    if node.left:
                        queue.append(node.left)
                    if node.right:
                        queue.append(node.right)
                    level.append(node)
                for i in range(len(level)-1):
                    level[i].next=level[i+1]
                level[-1].next = None
    

  • 0
    D

    You can only use constant space.


Log in to reply
 

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