117 - Populating Next Right Pointers in Each Node II
Written on November 9, 2015
Tweet
Follow up for problem “Populating Next Right Pointers in Each Node”. What if the given tree could be any binary tree? Would your previous solution still work?
class Solution:
def connect(self, root: 'Node') -> 'Node':
curr = dummy = Node(0)
node = root
while node:
curr.next = node.left
if curr.next:
curr = curr.next
curr.next = node.right
if curr.next:
curr = curr.next
node = node.next
if not node:
node = dummy.next
curr = dummy
return root