Even Odd Tree

MediumTreeslevel order

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Number the levels of a binary tree from 0 at the root. A tree is called **Even-Odd** when both of these hold: - On every **even-indexed** level, all values are **odd** and strictly **increasing** from left to right. - On every **odd-indexed** level, all values are **even** and strictly **decreasing** from left to right. Given the root of a non-empty binary tree, return true if it is Even-Odd, otherwise false. "Left to right" means the order in which a level-order traversal meets the nodes of that level, regardless of gaps. Check each level in a BFS for O(n) time.

Examples

Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
Output: true
Levels: [1] odd; [10,4] even and decreasing; [3,7,9] odd and increasing; [12,8,6,2] even and decreasing.
Input: root = [5,4,2,3,3,7]
Output: false
Level 2 is [3,3,7], which is not strictly increasing.
Input: root = [5,9,1,3,5,7]
Output: false
Level 1 holds odd values 9 and 1, but odd-indexed levels need even values.

Constraints

  • 1 <= number of nodes <= 10^5
  • 1 <= Node.val <= 10^6

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More level order problems