Advertisement
DeepRest

Populating Next Right Pointers in Each Node

Dec 28th, 2021
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. """
  2. # Definition for a Node.
  3. class Node:
  4.    def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
  5.        self.val = val
  6.        self.left = left
  7.        self.right = right
  8.        self.next = next
  9. """
  10. class Solution:
  11.     def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
  12.         if not root or not root.left:
  13.             return root
  14.         root.left.next = root.right
  15.         if root.next:
  16.             root.right.next = root.next.left
  17.            
  18.         self.connect(root.left)
  19.         self.connect(root.right)
  20.        
  21.         return root
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement