Practice/Meta/Leetcode 1609. Even Odd Tree
CodingOptional
Given the root of a binary tree, determine whether it satisfies specific alternating level constraints. The tree is considered valid if and only if it meets the following criteria:
Return true if the tree satisfies these constraints, otherwise return false.
Example 1:
Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2] Output: true Explanation: Level 0: [1] - all odd, no ordering issues (single element) Level 1: [10, 4] - all even, strictly decreasing (10 > 4) Level 2: [3, 7, 9] - all odd, strictly increasing (3 < 7 < 9) Level 3: [12, 8, 6, 2] - all even, strictly decreasing (12 > 8 > 6 > 2) All constraints are satisfied.
Example 2:
Input: root = [5,4,2,3,3,7] Output: false Explanation: Level 0: [5] - valid (odd) Level 1: [4, 2] - valid (even, decreasing) Level 2: [3, 3, 7] - invalid (contains duplicate 3, not strictly increasing)
Example 3:
Input: root = [2,3,5] Output: false Explanation: Level 0: [2] - invalid (even value at even-indexed level, should be odd)