Connect Same Level Siblings of a Binary Tree

Given a binary tree, at each level in the hierarchy, connect each node to the one on its right.

Statement

Given a binary tree, connect all nodes at the same hierarchical level. We need to connect them from left to right, such that the next pointer of each node points to the node on its immediate right. The next pointer of the right-most node at each level will be NULL.

For this problem, each node in the binary tree has one additional pointer (the next pointer), along with the left and right pointers.

Example

Let’s consider the following binary tree as an example:

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.