Connect nodes at the same level in a binary tree
PSEUDO ALGORITHM (Breadth first search approach)
We will use the same algorithm discussed in level-order-breadth-first-search-or-zig-zag-traversal-of-a-binary-tree with slight modification.
- Create an empty queue q
- Initialize roots height = 0; Enqueue q with the root node
- Loop while queue is not EMPTY
- temp_node = dequeue q
- set nextRight, for every node
- Enqueue temp_node’s children (first left then right children) to q with relevant height