leetcode icon indicating copy to clipboard operation
leetcode copied to clipboard

116. Populating Next Right Pointers in Each Node

Open tech-cow opened this issue 5 years ago • 0 comments

116 | Populating Next Right Pointers in Each Node

BFS Solution

class Solution(object):
    def connect(self, root):
        if not root:
            return 
        queue = [root]
        while queue:
            curr = queue.pop(0)
            if curr.left and curr.right:
                curr.left.next = curr.right
                if curr.next:
                    curr.right.next = curr.next.left
                queue.append(curr.left)
                queue.append(curr.right)

image


image

tech-cow avatar Mar 19 '20 21:03 tech-cow