Even Odd Tree

Medium

Topics
TreeBFS

A binary tree is even-odd if: on even-indexed levels (root is level 0) every node has an odd value and values are in strictly increasing order left to right; on odd-indexed levels every node has an even value and values are in strictly decreasing order. Return whether the tree is even-odd.

Example 1

123810167492
Every level satisfies both of its rules — parity and direction, both tied to whether the level index is even. A level-order walk checking each node against its left neighbour on the same level is enough, so the answer is true.
Tree given in level-order (breadth-first); null marks a missing child.
Input:  root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
Output: true

Example 2

343572
breaks a level rule
The shaded nodes break their level’s rule: even levels must hold odd values strictly increasing left to right, odd levels even values strictly decreasing. So the answer is false.
Input:  root = [5,4,2,3,3,7]
Output: false

Constraints

  • 1 <= number of nodes <= 10^5
  • 1 <= Node.val <= 10^6
Editorial — intuition, approach, complexity and pitfalls

Intuition

Two conditions per level, alternating by parity of depth: even levels hold odd values strictly increasing, odd levels hold even values strictly decreasing. Checking both requires seeing a level as a whole and in order, which is exactly what a level-by-level BFS provides.

Within a level, keep the previous value and verify two things per node: the value's own parity matches the level's requirement, and the monotonic direction holds against its predecessor. Both must be strict — equal neighbours fail, which is easy to miss.

Short-circuiting on the first violation is worth doing: a malformed tree can be rejected without visiting the rest.

Approach

  1. BFS level by level, tracking the level index.
  2. On an even level: every value must be odd and strictly greater than the previous.
  3. On an odd level: every value must be even and strictly less than the previous.

Dry Run

tree            1
              /   \
            10     4
           /  \      \
          3    7      9

level 0 (even): [1]           1 is odd ✓
level 1 (odd) : [10, 4]       both even ✓, 10 > 4 strictly decreasing ✓
level 2 (even): [3, 7, 9]     all odd ✓, 3 < 7 < 9 strictly increasing ✓

answer true

counterexample, level 2 = [3, 3, 9]:
  both odd, but 3 is not strictly greater than 3 -> false

Complexity

  • Time — O(n) worst case, less when a violation appears early.
  • Space — O(w): the queue holds one level.

Common Pitfalls

  • Using >= or <= for the monotonic test. Both directions are strict, so equal adjacent values are a violation.
  • Checking parity or monotonicity but not both. Each level needs the two conditions together.
  • Swapping the parities. Even-indexed levels hold odd values, which reads backwards and is the most common slip.
  • Comparing across a level boundary, by carrying the previous value into the next level instead of resetting it.
  • Mixing levels by reading the queue length inside the inner loop.

Full Solution — all languages

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def isEvenOddTree(self, root):
        from collections import deque
        q = deque([root]); level = 0
        while q:
            prev = None
            for _ in range(len(q)):
                n = q.popleft(); v = n.val
                if level % 2 == 0:
                    if v % 2 == 0 or (prev is not None and v <= prev): return False
                else:
                    if v % 2 == 1 or (prev is not None and v >= prev): return False
                prev = v
                if n.left: q.append(n.left)
                if n.right: q.append(n.right)
            level += 1
        return True
SensAI
Beta
Tap the mic to stop voice mode