1609. Even Odd Tree Medium

@problem@discussion
#Tree#Breadth-First Search#Binary Tree



1/**
2 * [1609] Even Odd Tree
3 *
4 * A binary tree is named Even-Odd if it meets the following conditions:
5 * 
6 * 	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.
7 * 	For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
8 * 	For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).
9 * 
10 * Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.
11 *  
12 * Example 1:
13 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/sample_1_1966.png" style="width: 362px; height: 229px;" />
14 * Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
15 * Output: true
16 * Explanation: The node values on each level are:
17 * Level 0: [1]
18 * Level 1: [10,4]
19 * Level 2: [3,7,9]
20 * Level 3: [12,8,6,2]
21 * 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.
22 * 
23 * Example 2:
24 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/15/sample_2_1966.png" style="width: 363px; height: 167px;" />
25 * Input: root = [5,4,2,3,3,7]
26 * Output: false
27 * Explanation: The node values on each level are:
28 * Level 0: [5]
29 * Level 1: [4,2]
30 * Level 2: [3,3,7]
31 * Node values in level 2 must be in strictly increasing order, so the tree is not Even-Odd.
32 * 
33 * Example 3:
34 * <img alt="" src="https://assets.leetcode.com/uploads/2020/09/22/sample_1_333_1966.png" style="width: 363px; height: 167px;" />
35 * Input: root = [5,9,1,3,5,7]
36 * Output: false
37 * Explanation: Node values in the level 1 should be even integers.
38 * 
39 *  
40 * Constraints:
41 * 
42 * 	The number of nodes in the tree is in the range [1, 10^5].
43 * 	1 <= Node.val <= 10^6
44 * 
45 */
46pub struct Solution {}
47use crate::util::tree::{TreeNode, to_tree};
48
49// problem: https://leetcode.com/problems/even-odd-tree/
50// discuss: https://leetcode.com/problems/even-odd-tree/discuss/?currentPage=1&orderBy=most_votes&query=
51
52// submission codes start here
53
54// Definition for a binary tree node.
55// #[derive(Debug, PartialEq, Eq)]
56// pub struct TreeNode {
57//   pub val: i32,
58//   pub left: Option<Rc<RefCell<TreeNode>>>,
59//   pub right: Option<Rc<RefCell<TreeNode>>>,
60// }
61// 
62// impl TreeNode {
63//   #[inline]
64//   pub fn new(val: i32) -> Self {
65//     TreeNode {
66//       val,
67//       left: None,
68//       right: None
69//     }
70//   }
71// }
72use std::rc::Rc;
73use std::cell::RefCell;
74impl Solution {
75    pub fn is_even_odd_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
76        false
77    }
78}
79
80// submission codes end
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn test_1609() {
88    }
89}
90


Back
© 2025 bowen.ge All Rights Reserved.