Reverse Odd Levels of a Perfect Tree
You are given the root of a binary tree. The tree is perfect (every level is completely filled). The root is on level 0. For every odd level (1, 3, 5, ...), reverse the order of the values on that level from left to right, so the first node gets the last node's value and so on. The shape does not change. Return the root.
Trees appear in the tests in level order, with null for a missing child, like [3,9,20,null,null,15,7]. Each node is a TreeNode with .val, .left and .right; the class is already defined for you.
Example 1
root [2,3,5,8,13,21,34]
returns [2,5,3,8,13,21,34]
Level 1 [3,5] becomes [5,3]: the result is [2,5,3,8,13,21,34].
Constraints
0 <= number of nodes <= 100, and the tree is perfect0 <= node.val <= 100
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.