Back to problems

Connect next pointers for each tree level

Algorithm · Meta · Hard

Let a binary tree be given in which every node has the usual left and right child references plus a next pointer initially set to null. The tree can be unbalanced and incomplete. For each non-null node, assign its next pointer to the closest non-null node located to its right at the same depth. When no such node exists, keep next as null. The input is provided as a level-order array using heap indexing: a node at array position i has its left child at position 2i + 1 and its…

Checking your access…