Created
September 18, 2020 09:36
-
-
Save iamprayush/a14685fb7df10a197328ef1ef7b54a5b to your computer and use it in GitHub Desktop.
Populating Next Right Pointers in Each Node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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