|
| 1 | +--- |
| 2 | +id: even-odd-tree |
| 3 | +title: Even Odd Tree (LeetCode) |
| 4 | +sidebar_label: 1609-EvenOddTree |
| 5 | +tags: |
| 6 | + - Tree |
| 7 | + - Breadth-First Search |
| 8 | + - Level Order Traversal |
| 9 | +description: Determine if a binary tree is an "even-odd" tree, where the level of nodes alternates between odd and even values. |
| 10 | +sidebar_position: 1609 |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 16 | +| :---------------- | :------------ | :--------------- | |
| 17 | +| [Even Odd Tree](https://leetcode.com/problems/even-odd-tree/) | [Even Odd Tree Solution on LeetCode](https://leetcode.com/problems/even-odd-tree/solutions/) | [vaishu_1904](https://leetcode.com/u/vaishu_1904/) | |
| 18 | + |
| 19 | +## Problem Description |
| 20 | + |
| 21 | +A binary tree is named "even-odd" if it satisfies the following conditions: |
| 22 | + |
| 23 | +1. The root has an odd value. |
| 24 | +2. Each level has nodes with values that alternate between odd and even. |
| 25 | +3. For each level, nodes with odd values come first, followed by nodes with even values. |
| 26 | + |
| 27 | +Given the root of a binary tree, return `true` if the binary tree is "even-odd", otherwise return `false`. |
| 28 | + |
| 29 | +### Example 1 |
| 30 | + |
| 31 | +- **Input:** `root = [1,10,4,3,null,7,9,12,8,6,null,null,2]` |
| 32 | +- **Output:** `true` |
| 33 | +- **Explanation:** The values of each level are: |
| 34 | + - Level 0: [1] |
| 35 | + - Level 1: [10, 4] |
| 36 | + - Level 2: [3, 7, 9, 12] |
| 37 | + - Level 3: [8, 6, 2] |
| 38 | + The tree satisfies all the conditions for an "even-odd" tree. |
| 39 | + |
| 40 | +### Example 2 |
| 41 | + |
| 42 | +- **Input:** `root = [5,4,2,3,3,7]` |
| 43 | +- **Output:** `false` |
| 44 | +- **Explanation:** The values of the levels are: |
| 45 | + - Level 0: [5] |
| 46 | + - Level 1: [4, 2] |
| 47 | + - Level 2: [3, 3, 7] |
| 48 | + Node values in the second level are not in strictly increasing order. |
| 49 | + |
| 50 | +### Constraints |
| 51 | + |
| 52 | +- The number of nodes in the tree is in the range `[1, 10^5]`. |
| 53 | +- `1 <= Node.val <= 10^6` |
| 54 | + |
| 55 | +## Approach |
| 56 | + |
| 57 | +To determine if a binary tree is an "even-odd" tree, we can use a breadth-first search (BFS) approach to traverse the tree level by level. Here's the approach: |
| 58 | + |
| 59 | +1. **Breadth-First Search (BFS)**: |
| 60 | + - Use a queue to perform level-order traversal. |
| 61 | + - For each level, check if the values alternate between odd and even, maintaining strict ordering. |
| 62 | + |
| 63 | +2. **Validation**: |
| 64 | + - For each node at an even level (`level % 2 == 0`), values should be strictly increasing and odd. |
| 65 | + - For each node at an odd level (`level % 2 != 0`), values should be strictly decreasing and even. |
| 66 | + |
| 67 | +3. **Edge Cases**: |
| 68 | + - Handle single node trees appropriately. |
| 69 | + |
| 70 | +### Solution Code |
| 71 | + |
| 72 | +#### Python |
| 73 | + |
| 74 | +```python |
| 75 | +from collections import deque |
| 76 | +import math |
| 77 | + |
| 78 | +class Solution: |
| 79 | + def isEvenOddTree(self, root: TreeNode) -> bool: |
| 80 | + if not root: |
| 81 | + return False |
| 82 | + |
| 83 | + queue = deque([root]) |
| 84 | + level = 0 |
| 85 | + |
| 86 | + while queue: |
| 87 | + size = len(queue) |
| 88 | + prev_val = -math.inf if level % 2 == 0 else math.inf |
| 89 | + |
| 90 | + for _ in range(size): |
| 91 | + node = queue.popleft() |
| 92 | + |
| 93 | + if level % 2 == 0: |
| 94 | + if node.val % 2 == 0 or node.val <= prev_val: |
| 95 | + return False |
| 96 | + else: |
| 97 | + if node.val % 2 != 0 or node.val >= prev_val: |
| 98 | + return False |
| 99 | + |
| 100 | + prev_val = node.val |
| 101 | + |
| 102 | + if node.left: |
| 103 | + queue.append(node.left) |
| 104 | + if node.right: |
| 105 | + queue.append(node.right) |
| 106 | + |
| 107 | + level += 1 |
| 108 | + |
| 109 | + return True |
| 110 | +``` |
| 111 | + |
| 112 | +#### java |
| 113 | +```java |
| 114 | +import java.util.LinkedList; |
| 115 | +import java.util.Queue; |
| 116 | + |
| 117 | +class Solution { |
| 118 | + public boolean isEvenOddTree(TreeNode root) { |
| 119 | + if (root == null) return false; |
| 120 | + |
| 121 | + Queue<TreeNode> queue = new LinkedList<>(); |
| 122 | + queue.offer(root); |
| 123 | + int level = 0; |
| 124 | + |
| 125 | + while (!queue.isEmpty()) { |
| 126 | + int size = queue.size(); |
| 127 | + int prevVal = (level % 2 == 0) ? Integer.MIN_VALUE : Integer.MAX_VALUE; |
| 128 | + |
| 129 | + for (int i = 0; i < size; i++) { |
| 130 | + TreeNode node = queue.poll(); |
| 131 | + |
| 132 | + if (level % 2 == 0) { |
| 133 | + if (node.val % 2 == 0 || node.val <= prevVal) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + } else { |
| 137 | + if (node.val % 2 != 0 || node.val >= prevVal) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + prevVal = node.val; |
| 143 | + |
| 144 | + if (node.left != null) { |
| 145 | + queue.offer(node.left); |
| 146 | + } |
| 147 | + if (node.right != null) { |
| 148 | + queue.offer(node.right); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + level++; |
| 153 | + } |
| 154 | + |
| 155 | + return true; |
| 156 | + } |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +#### C++ |
| 161 | +```c++ |
| 162 | +#include <queue> |
| 163 | +#include <limits> |
| 164 | +using namespace std; |
| 165 | + |
| 166 | +class Solution { |
| 167 | +public: |
| 168 | + bool isEvenOddTree(TreeNode* root) { |
| 169 | + if (!root) return false; |
| 170 | + |
| 171 | + queue<TreeNode*> q; |
| 172 | + q.push(root); |
| 173 | + int level = 0; |
| 174 | + |
| 175 | + while (!q.empty()) { |
| 176 | + int size = q.size(); |
| 177 | + int prevVal = (level % 2 == 0) ? numeric_limits<int>::min() : numeric_limits<int>::max(); |
| 178 | + |
| 179 | + for (int i = 0; i < size; ++i) { |
| 180 | + TreeNode* node = q.front(); |
| 181 | + q.pop(); |
| 182 | + |
| 183 | + if (level % 2 == 0) { |
| 184 | + if (node->val % 2 == 0 || node->val <= prevVal) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + } else { |
| 188 | + if (node->val % 2 != 0 || node->val >= prevVal) { |
| 189 | + return false; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + prevVal = node->val; |
| 194 | + |
| 195 | + if (node->left) q.push(node->left); |
| 196 | + if (node->right) q.push(node->right); |
| 197 | + } |
| 198 | + |
| 199 | + ++level; |
| 200 | + } |
| 201 | + |
| 202 | + return true; |
| 203 | + } |
| 204 | +}; |
| 205 | +``` |
| 206 | +
|
| 207 | +#### Conclusion |
| 208 | +The above solutions use BFS to validate if a given binary tree is an "even-odd" tree based on the |
| 209 | +defined rules. Each solution ensures that nodes at even and odd levels have values that strictly |
| 210 | +alternate in the required manner. Adjustments for different languages and edge cases are handled to |
| 211 | +ensure robustness across different inputs. |
0 commit comments