Interview Questions/Coding/Odd-Level Memo Tree Reversal

Odd-Level Memo Tree Reversal

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Odd-Level Memo Tree Reversal

A perfect binary memo tree is stored as a level-order array. The root is level 0, its children are level 1, and so on. For every odd-numbered level, reverse the values within that level. The tree structure itself does not change; only the values appearing at those positions are rearranged. Input Format: You are given an integer array levelOrder representing a perfect binary tree. Output Format: Return the level-order array after reversing all odd levels.

Examples

Example 1
Input:
levelOrder = [2, 3, 5, 8, 13, 21, 34]
Output:
2 5 3 8 13 21 34

Explanation: level 1 [3, 5] -> [5, 3]. The final level order is 2 5 3 8 13 21 34.

Approach hint

Look for the state that actually matters after each operation.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp