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
Populating Next Right Pointers in Each Node II LeetCode Solution
Table of Contents
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 Solution C++
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 Solution Java
class Solution {
public Node connect(Node root) {
if (root == null || (root.left == null && root.right == null)) {
return root;
}
if (root.left != null && root.right != null) {
root.left.next = root.right;
root.right.next = getNextHasChildrenNode(root);
}
if (root.left == null) {
root.right.next = getNextHasChildrenNode(root);
}
if (root.right == null) {
root.left.next = getNextHasChildrenNode(root);
}
root.right = connect(root.right);
root.left = connect(root.left);
return root;
}
public Node getNextHasChildrenNode(Node root) {
while (root.next != null) {
if (root.next.left != null) {
return root.next.left;
}
if (root.next.right != null) {
return root.next.right;
}
root = root.next;
}
return null;
}
}
Code language: PHP (php)
Populating Next Right Pointers in Each Node II Solution JavaScript
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 Solution Python
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