Connect All Siblings of a Binary Tree
Given a binary tree, connect its nodes so that it allows a level-order traversal of the tree.
Statement
The task is to connect all nodes at the same hierarchical level. 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 should point to the first node of the next level in the tree.
Each node in the given binary tree for this problem contains the next
pointer, along with the left and right pointers. The next
pointer is used to connect the same level nodes to each other and across levels.
Example
Consider the following binary tree:
Here’s how the final tree looks when all the next pointers are connected:
Sample input
The input list below represents the level-order traversal of the binary tree, while the value that follows represents the node whose next node we need to find:
[100, 50, 200, 25, 75, 125, 300, 10, 350, 15], 200
Expected output
Only if the next pointer of the node with value 200 is correctly set up will the correct result, 25, be returned.
25
Try it yourself
...