Skip to content

Instantly share code, notes, and snippets.

@iamprayush
Created September 18, 2020 09:36
Show Gist options
  • Save iamprayush/a14685fb7df10a197328ef1ef7b54a5b to your computer and use it in GitHub Desktop.
Save iamprayush/a14685fb7df10a197328ef1ef7b54a5b to your computer and use it in GitHub Desktop.
Populating Next Right Pointers in Each Node
class Solution:
def connect(self, node):
if not node:
return None
# For every node...
# 1. We will connect it's left child to it's right child. (If it has children).
if node.left and node.right:
node.left.next = node.right
# 2. Connect it's right child to the current node's next's left child. (If it has a next node).
if node.next:
node.right.next = node.next.left
self.connect(node.left)
self.connect(node.right)
return node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment