117. Populating Next Right Pointers in Each Node II
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to populate the next pointer for each node in a given binary tree. This next pointer should point to the node's immediate right neighbor on the same level. If no such neighbor exists, the next pointer should be set to NULL. Unlike its predecessor problem ("Populating Next Right Pointers in Each Node"), this problem deals with a general binary tree, not necessarily a perfect one. The key constraint is the follow-up requirement to use only constant extra space.
Solution
Populating Next Right Pointers in Each Node II in 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: 'Node') -> 'Node':
if not root:
return None
# level_start is the first node of the current level we are processing.
level_start = root
while level_start:
# dummy_head is a placeholder for the start of the next level.
dummy_head = Node(0)
# tail is used to connect nodes in the next level.
tail = dummy_head
# curr iterates through the current level using established next pointers.
curr = level_start
while curr:
# If the current node has a left child, link it.
if curr.left:
tail.next = curr.left
tail = tail.next
# If the current node has a right child, link it.
if curr.right:
tail.next = curr.right
tail = tail.next
# Move to the next node in the current level.
curr = curr.next
# Move to the next level. The start of the next level is the first child
# we linked, which is pointed to by dummy_head.next.
level_start = dummy_head.next
return root
Populating Next Right Pointers in Each Node II in Java
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
if (root == null) {
return null;
}
// level_start is the first node of the current level we are processing.
Node level_start = root;
while (level_start != null) {
// dummy_head is a placeholder for the start of the next level.
Node dummy_head = new Node(0);
// tail is used to connect nodes in the next level.
Node tail = dummy_head;
// curr iterates through the current level using established next pointers.
Node curr = level_start;
while (curr != null) {
// If the current node has a left child, link it.
if (curr.left != null) {
tail.next = curr.left;
tail = tail.next;
}
// If the current node has a right child, link it.
if (curr.right != null) {
tail.next = curr.right;
tail = tail.next;
}
// Move to the next node in the current level.
curr = curr.next;
}
// Move to the next level. The start of the next level is the first child
// we linked, which is pointed to by dummy_head.next.
level_start = dummy_head.next;
}
return root;
}
}
Populating Next Right Pointers in Each Node II in C++
/*
// Definition for a Node.
class Node {
public:
int val;
Node* left;
Node* right;
Node* next;
Node() : val(0), left(NULL), right(NULL), next(NULL) {}
Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
Node(int _val, Node* _left, Node* _right, Node* _next)
: val(_val), left(_left), right(_right), next(_next) {}
};
*/
class Solution {
public:
Node* connect(Node* root) {
if (!root) {
return nullptr;
}
// level_start is the first node of the current level we are processing.
Node* level_start = root;
while (level_start) {
// A dummy node to simplify handling the start of the next level.
// Using a stack-allocated node is efficient and avoids memory management issues.
Node dummy(0);
Node* tail = &dummy;
// curr iterates through the current level using established next pointers.
Node* curr = level_start;
while (curr) {
// If the current node has a left child, link it.
if (curr->left) {
tail->next = curr->left;
tail = tail->next;
}
// If the current node has a right child, link it.
if (curr->right) {
tail->next = curr->right;
tail = tail->next;
}
// Move to the next node in the current level.
curr = curr->next;
}
// Move to the next level. The start of the next level is the first child
// we linked, which is pointed to by dummy.next.
level_start = dummy.next;
}
return root;
}
};
Populating Next Right Pointers in Each Node II in TypeScript
/**
* Definition for _Node.
* class _Node {
* val: number
* left: _Node | null
* right: _Node | null
* next: _Node | null
*
* constructor(val?: number, left?: _Node, right?: _Node, next?: _Node) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function connect(root: _Node | null): _Node | null {
if (!root) {
return null;
}
// level_start is the first node of the current level we are processing.
let level_start: _Node | null = root;
while (level_start) {
// dummy_head is a placeholder for the start of the next level.
const dummy_head = new _Node(0);
// tail is used to connect nodes in the next level.
let tail: _Node = dummy_head;
// curr iterates through the current level using established next pointers.
let curr: _Node | null = level_start;
while (curr) {
// If the current node has a left child, link it.
if (curr.left) {
tail.next = curr.left;
tail = tail.next;
}
// If the current node has a right child, link it.
if (curr.right) {
tail.next = curr.right;
tail = tail.next;
}
// Move to the next node in the current level.
curr = curr.next;
}
// Move to the next level. The start of the next level is the first child
// we linked, which is pointed to by dummy_head.next.
level_start = dummy_head.next;
}
return root;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.