• 0

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.

  1. Create an empty queue q
  2. Initialize roots height = 0; Enqueue q with the root node
  3. Loop while queue is not EMPTY
    1. temp_node = dequeue q
    2. set nextRight, for every node
    3. Enqueue temp_node’s children (first left then right children) to q with relevant height


Solution