# Populating Next Right Pointers in Each Node II LeetCode Solution

Here, We see Populating Next Right Pointers in Each Node II LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

# List of all LeetCode Solution

## Problem Statement

Given a binary treestruct Node { int val; Node *left; Node *right; Node *next; }

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to `NULL`.

Initially, all next pointers are set to `NULL`.

Example 1:

Input: root = [1,2,3,4,5,null,7]
Output: [1,#,2,3,#,4,5,7,#]
Explanation: Given the above binary tree (Figure A), your function should populate each next pointer to point to its next right node, just like in Figure B. The serialized output is in level order as connected by the next pointers, with ‘#’ signifying the end of each level.

Example 2:
Input: root = []
Output: []

## Populating Next Right Pointers in Each Node II LeetCode SolutionC++

``````class Solution {
public:
Node* connect(Node* root) {
Node *currParent = root, *baseChild, *currChild, *nextChild;
while (currParent) {
while (currParent->next && !currParent->left && !currParent->right) currParent = currParent->next;
currChild = baseChild = currParent->left ? currParent->left : currParent->right;
while (currChild) {
if (currParent->right && currChild != currParent->right) nextChild = currParent->right;
else {
currParent = currParent->next;
while (currParent && !currParent->left && !currParent->right) currParent = currParent->next;
nextChild = currParent ? currParent->left ? currParent->left : currParent->right : currParent;
}
currChild->next = nextChild;
currChild = nextChild;
}
currParent = baseChild;
}
return root;
}
};```Code language: PHP (php)```

## Populating Next Right Pointers in Each Node II LeetCode SolutionJava

``````class Solution {
public Node connect(Node root) {
if (root == null) {
return root;
}
queue.offer(root);
while (!queue.isEmpty()) {
int levelCount = queue.size();
Node prev = null;
for (int i = 0; i < levelCount; i++) {
Node curNode = queue.poll();
if (prev != null) {
prev.next = curNode;
}
prev = curNode;
if (curNode.left != null) {
}
if (curNode.right != null) {
}
}
}
return root;
}
}```Code language: PHP (php)```

## Populating Next Right Pointers in Each Node II SolutionJavaScript

``````var connect = function(root) {
if (!root) return root;
let queue = [root];
let tempQueue = [];
while(queue.length){
let curr = queue.splice(0, 1)[0];
let {left, right} = curr;
if (left) tempQueue.push(left);
if (right) tempQueue.push(right);
if (queue.length === 0){
curr.next = null;
queue = tempQueue;
tempQueue = [];
}else{
curr.next = queue[0];
}
}
return root;
};```Code language: JavaScript (javascript)```

## Populating Next Right Pointers in Each Node II SolutionPython

``````class Solution(object):
def connect(self, root):
if not root:
return None
q = deque()
q.append(root)
dummy=Node(-999)
while q:
length=len(q)
prev=dummy
for _ in range(length):
popped=q.popleft()
if popped.left:
q.append(popped.left)
prev.next=popped.left
prev=prev.next
if popped.right:
q.append(popped.right)
prev.next=popped.right
prev=prev.next
return root``````
Scroll to Top