Advertisement
DeepRest

Populating Next Right Pointers in Each Node II

May 13th, 2022
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. class Solution:
  2.     def connect(self, root: 'Node') -> 'Node':
  3.         head = root
  4.         while head is not None:
  5.             parent = head  
  6.             child = head = Node()
  7.            
  8.             while parent is not None:
  9.                 if parent.left is not None:
  10.                     child.next = parent.left
  11.                     child = child.next
  12.                    
  13.                 if parent.right is not None:
  14.                     child.next = parent.right
  15.                     child = child.next
  16.                
  17.                 parent = parent.next
  18.            
  19.             head = head.next
  20.        
  21.         return root
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement