|
| 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 | + */ |
| 46 | +pub struct Solution {} |
| 47 | +use crate::util::tree::{to_tree, TreeNode}; |
| 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 | +// } |
| 72 | +use std::cell::RefCell; |
| 73 | +use std::rc::Rc; |
| 74 | +impl Solution { |
| 75 | + pub fn is_even_odd_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool { |
| 76 | + let mut levels_val = vec![]; |
| 77 | + let mut to_vst = vec![(root.unwrap(), 1)]; |
| 78 | + |
| 79 | + while !to_vst.is_empty() { |
| 80 | + let (node, depth) = to_vst.pop().unwrap(); |
| 81 | + let node = node.borrow(); |
| 82 | + if let Some(right) = node.right.clone() { |
| 83 | + to_vst.push((right, depth + 1)); |
| 84 | + } |
| 85 | + if let Some(left) = node.left.clone() { |
| 86 | + to_vst.push((left, depth + 1)); |
| 87 | + } |
| 88 | + |
| 89 | + if (depth % 2 == 0) ^ (node.val % 2 == 0) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + if levels_val.len() < depth { |
| 94 | + levels_val.push(node.val); |
| 95 | + } else { |
| 96 | + if node.val == levels_val[depth - 1] |
| 97 | + || (depth % 2 == 0) ^ (node.val < levels_val[depth - 1]) |
| 98 | + { |
| 99 | + return false; |
| 100 | + } |
| 101 | + } |
| 102 | + levels_val[depth - 1] = node.val; |
| 103 | + } |
| 104 | + |
| 105 | + true |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// submission codes end |
| 110 | + |
| 111 | +#[cfg(test)] |
| 112 | +mod tests { |
| 113 | + use super::*; |
| 114 | + |
| 115 | + #[test] |
| 116 | + fn test_1609_example_1() { |
| 117 | + let root = tree![1, 10, 4, 3, null, 7, 9, 12, 8, 6, null, null, 2]; |
| 118 | + |
| 119 | + let result = true; |
| 120 | + |
| 121 | + assert_eq!(Solution::is_even_odd_tree(root), result); |
| 122 | + } |
| 123 | + |
| 124 | + #[test] |
| 125 | + fn test_1609_example_2() { |
| 126 | + let root = tree![5, 4, 2, 3, 3, 7]; |
| 127 | + |
| 128 | + let result = false; |
| 129 | + |
| 130 | + assert_eq!(Solution::is_even_odd_tree(root), result); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn test_1609_example_3() { |
| 135 | + let root = tree![5, 9, 1, 3, 5, 7]; |
| 136 | + |
| 137 | + let result = false; |
| 138 | + |
| 139 | + assert_eq!(Solution::is_even_odd_tree(root), result); |
| 140 | + } |
| 141 | +} |
0 commit comments