A binary tree is named Even-Odd if it meets the following conditions:
0, its children are at level index 1, their children are at level index 2, etc.Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.
Example 1:
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:
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:
Input: root = [5,9,1,3,5,7] Output: false Explanation: Node values in the level 1 should be even integers.
Constraints:
[1, 105].1 <= Node.val <= 106When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method for the Even Odd Tree problem means we're going to check if the tree follows the rules, level by level. We will inspect every single node at each level to ensure it adheres to the even-odd characteristics required.
Here's how the algorithm would work step-by-step:
from collections import deque
def is_even_odd_tree(root):
if not root:
return True
queue = deque([root])
level = 0
while queue:
level_size = len(queue)
previous_value = None
for _ in range(level_size):
node = queue.popleft()
# Check even/odd property based on level
if level % 2 == 0:
if node.val % 2 == 0:
return False
# Check increasing order for even levels
if previous_value is not None and node.val <= previous_value:
return False
else:
if node.val % 2 != 0:
return False
# Check decreasing order for odd levels
if previous_value is not None and node.val >= previous_value:
return False
previous_value = node.val
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
level += 1
return TrueThis problem requires us to check if a tree has alternating properties on each level. We use a level-by-level exploration to make sure each node meets the requirements of its level: even levels have odd values in increasing order, and odd levels have even values in decreasing order.
Here's how the algorithm would work step-by-step:
from collections import deque
def is_even_odd_tree(root):
if not root:
return True
queue = deque([root])
level = 0
while queue:
level_size = len(queue)
previous_node_value = -1 if level % 2 == 0 else float('inf')
for _ in range(level_size):
current_node = queue.popleft()
# Check odd/even property based on level.
if level % 2 == 0:
if current_node.val % 2 == 0:
return False
# Check increasing order for even levels.
if current_node.val <= previous_node_value:
return False
else:
if current_node.val % 2 != 0:
return False
# Check decreasing order for odd levels.
if current_node.val >= previous_node_value:
return False
previous_node_value = current_node.val
if current_node.left:
queue.append(current_node.left)
if current_node.right:
queue.append(current_node.right)
level += 1
# If all nodes satisfy conditions, return True.
return True| Case | How to Handle |
|---|---|
| Null root node | Return true immediately since an empty tree technically satisfies the level conditions. |
| Single node tree with root value violating level constraints | Check root node value parity against level parity (level 0 should be odd) and return false if violated. |
| Skewed tree with very deep levels | Ensure BFS implementation avoids excessive memory usage and stack overflow errors. |
| Tree with all nodes having same value. | The level-wise checks for strictly increasing/decreasing values will detect the violation and return false. |
| Large integer values causing potential overflow in comparison. | Use appropriate data types (e.g., long) if node values can be extremely large to prevent overflow during comparisons. |
| Level with only one node | Ensure increasing/decreasing checks don't cause errors; single node automatically satisfies the condition. |
| Level with non-consecutive even or odd numbers (e.g., 1, 5, 9 or 2, 6, 10) | The increasing/decreasing check correctly handles non-consecutive sequences if they are strictly increasing/decreasing. |
| Tree with negative node values. | The parity check and increasing/decreasing checks should work correctly with negative numbers. |