> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Even Odd Tree Python Solution with Tests

> Tested Python solution for LeetCode 1609 with 19 pytest cases. Generate a practice environment with lcpy.

LeetCode 1609, [Medium](/catalog/medium). Topics: [Tree](/catalog/topics/tree), [Breadth-First Search](/catalog/topics/breadth-first-search), [Binary Tree](/catalog/topics/binary-tree). [View on LeetCode](https://leetcode.com/problems/even-odd-tree/description/).

Generate this problem as a practice environment: tested reference solution, 19 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1609   # by problem number
lcpy gen -s even_odd_tree   # by problem name
```

## Problem

A binary tree is named Even-Odd if it meets the following conditions:

* The root of the binary tree is at level index `0`, its children are at level index `1`, their children are at level index `2`, etc.
* For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
* For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).

Given the `root` of a binary tree, return `true` if the binary tree is Even-Odd, otherwise return `false`.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2020/09/15/sample_1_1966.png)

```
Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
Output: true
Explanation: The node values on each level are:
Level 0: [1]
Level 1: [10,4]
Level 2: [3,7,9]
Level 3: [12,8,6,2]
Since levels 0 and 2 are all odd and increasing and levels 1 and 3 are all even and decreasing, the tree is Even-Odd.
```

![Example 2](https://assets.leetcode.com/uploads/2020/09/15/sample_2_1966.png)

```
Input: root = [5,4,2,3,3,7]
Output: false
Explanation: The node values on each level are:
Level 0: [5]
Level 1: [4,2]
Level 2: [3,3,7]
Node values in level 2 must be in strictly increasing order, so the tree is not Even-Odd.
```

![Example 3](https://assets.leetcode.com/uploads/2020/09/22/sample_1_333_1966.png)

```
Input: root = [5,9,1,3,5,7]
Output: false
Explanation: Node values in the level 1 should be even integers.
```

### Constraints

* The number of nodes in the tree is in the range \[1, 10^5].
* 1 \<= Node.val \<= 10^6

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/even_odd_tree/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/even_odd_tree/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from leetcode_py import TreeNode


class Solution:
    # Time: O(n)
    # Space: O(w) where w is the max level width
    def is_even_odd_tree(self, root: TreeNode[int] | None) -> bool:
        if root is None:
            return False
        level = [root]
        depth = 0
        while level:
            prev = None
            for node in level:
                val = node.val
                if depth % 2 == 0:
                    if val % 2 == 0:
                        return False
                    if prev is not None and val <= prev:
                        return False
                else:
                    if val % 2 == 1:
                        return False
                    if prev is not None and val >= prev:
                        return False
                prev = val
            level = [child for node in level for child in (node.left, node.right) if child]
            depth += 1
        return True
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(w) where w is the max level width |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.