# Populating Next Right Pointers in Each Node (Leetcode 116)

Problem Link: <https://leetcode.com/problems/populating-next-right-pointers-in-each-node/>

```python
"""
# Definition for a Node.
class Node:
    def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
        self.val = val
        self.left = left
        self.right = right
        self.next = next
"""

class Solution:
    def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
        if(root is None):
            return None
        q=[root]
        while(len(q)>0):
            temp=[]
            for i in range(len(q)):
                node=q.pop()
                if(node.left is not None):
                    q.insert(0,node.left)
                if(node.right is not None):
                    q.insert(0,node.right)
                temp.append(node)
            for i in range(len(temp)):
                if(i==len(temp)-1):
                    temp[i].next=None
                else:
                    temp[i].next=temp[i+1]
        return root      
```
