Even Odd Tree
Medium
Topics
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
true.null marks a missing child.Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2] Output: true
Example 2
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
- BFS level by level, tracking the level index.
- On an even level: every value must be odd and strictly greater than the previous.
- 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 -> falseComplexity
- 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 Trueimport java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) {
val = v;
}
}
class Solution {
public boolean isEvenOddTree(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
q.add(root);
int level = 0;
while (!q.isEmpty()) {
int sz = q.size();
boolean has = false;
int prev = 0;
for (int i = 0; i < sz; i++) {
TreeNode n = q.poll();
int v = n.val;
if (level % 2 == 0) {
if (v % 2 == 0 || (has && v <= prev)) return false;
} else {
if (v % 2 == 1 || (has && v >= prev)) return false;
}
prev = v;
has = true;
if (n.left != null) q.add(n.left);
if (n.right != null) q.add(n.right);
}
level++;
}
return true;
}
}#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {
}
};
class Solution {
public: bool isEvenOddTree(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
int level = 0;
while (!q.empty()) {
int sz = q.size();
bool has = false;
int prev = 0;
for (int i = 0; i < sz; i++) {
TreeNode* n = q.front();
q.pop();
int v = n->val;
if (level % 2 == 0) {
if (v % 2 == 0 || (has && v <= prev)) return false;
} else {
if (v % 2 == 1 || (has && v >= prev)) return false;
}
prev = v;
has = true;
if (n->left) q.push(n->left);
if (n->right) q.push(n->right);
}
level++;
}
return true;
}
};/**
* Tree node shape: { val, left, right }
* @param {TreeNode} root
* @return {boolean}
*/ var isEvenOddTree = function(root) {
let q = [root], level = 0;
while (q.length) {
let prev = null;
const nq = [];
for (const n of q) {
const v = n.val;
if (level % 2 === 0) {
if (v % 2 === 0 || (prev !== null && v <= prev)) return false;
} else {
if (v % 2 === 1 || (prev !== null && v >= prev)) return false;
}
prev = v;
if (n.left) nq.push(n.left);
if (n.right) nq.push(n.right);
}
q = nq;
level++;
}
return true;
};