Skip to content

Commit 7d40be5

Browse files
committed
2415. Reverse Odd Levels of Binary Tree: AC
1 parent bc4768e commit 7d40be5

2 files changed

Lines changed: 159 additions & 0 deletions

File tree

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1831,3 +1831,4 @@ mod s2411_smallest_subarrays_with_maximum_bitwise_or;
18311831
mod s2412_minimum_money_required_before_transactions;
18321832
mod s2413_smallest_even_multiple;
18331833
mod s2414_length_of_the_longest_alphabetical_continuous_substring;
1834+
mod s2415_reverse_odd_levels_of_binary_tree;
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/**
2+
* [2415] Reverse Odd Levels of Binary Tree
3+
*
4+
* Given the root of a perfect binary tree, reverse the node values at each odd level of the tree.
5+
*
6+
* For example, suppose the node values at level 3 are [2,1,3,4,7,11,29,18], then it should become [18,29,11,7,4,3,1,2].
7+
*
8+
* Return the root of the reversed tree.
9+
* A binary tree is perfect if all parent nodes have two children and all leaves are on the same level.
10+
* The level of a node is the number of edges along the path between it and the root node.
11+
*
12+
* Example 1:
13+
* <img alt="" src="https://assets.leetcode.com/uploads/2022/07/28/first_case1.png" style="width: 626px; height: 191px;" />
14+
* Input: root = [2,3,5,8,13,21,34]
15+
* Output: [2,5,3,8,13,21,34]
16+
* Explanation:
17+
* The tree has only one odd level.
18+
* The nodes at level 1 are 3, 5 respectively, which are reversed and become 5, 3.
19+
*
20+
* Example 2:
21+
* <img alt="" src="https://assets.leetcode.com/uploads/2022/07/28/second_case3.png" style="width: 591px; height: 111px;" />
22+
* Input: root = [7,13,11]
23+
* Output: [7,11,13]
24+
* Explanation:
25+
* The nodes at level 1 are 13, 11, which are reversed and become 11, 13.
26+
*
27+
* Example 3:
28+
*
29+
* Input: root = [0,1,2,0,0,0,0,1,1,1,1,2,2,2,2]
30+
* Output: [0,2,1,0,0,0,0,2,2,2,2,1,1,1,1]
31+
* Explanation:
32+
* The odd levels have non-zero values.
33+
* The nodes at level 1 were 1, 2, and are 2, 1 after the reversal.
34+
* The nodes at level 3 were 1, 1, 1, 1, 2, 2, 2, 2, and are 2, 2, 2, 2, 1, 1, 1, 1 after the reversal.
35+
*
36+
*
37+
* Constraints:
38+
*
39+
* The number of nodes in the tree is in the range [1, 2^14].
40+
* 0 <= Node.val <= 10^5
41+
* root is a perfect binary tree.
42+
*
43+
*/
44+
pub struct Solution {}
45+
use crate::util::tree::{TreeNode, to_tree};
46+
47+
// problem: https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/
48+
// discuss: https://leetcode.com/problems/reverse-odd-levels-of-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
49+
50+
// submission codes start here
51+
52+
// Definition for a binary tree node.
53+
// #[derive(Debug, PartialEq, Eq)]
54+
// pub struct TreeNode {
55+
// pub val: i32,
56+
// pub left: Option<Rc<RefCell<TreeNode>>>,
57+
// pub right: Option<Rc<RefCell<TreeNode>>>,
58+
// }
59+
//
60+
// impl TreeNode {
61+
// #[inline]
62+
// pub fn new(val: i32) -> Self {
63+
// TreeNode {
64+
// val,
65+
// left: None,
66+
// right: None
67+
// }
68+
// }
69+
// }
70+
use std::cell::RefCell;
71+
use std::rc::Rc;
72+
impl Solution {
73+
pub fn reverse_odd_levels(
74+
root: Option<Rc<RefCell<TreeNode>>>,
75+
) -> Option<Rc<RefCell<TreeNode>>> {
76+
Self::dfs_helper(root.clone(), None, 0);
77+
78+
root
79+
}
80+
81+
fn dfs_helper(
82+
left: Option<Rc<RefCell<TreeNode>>>,
83+
right: Option<Rc<RefCell<TreeNode>>>,
84+
level: i32,
85+
) {
86+
let rev = level & 0x01 == 1;
87+
match (left.clone(), right.clone()) {
88+
(Some(l_node), None) => {
89+
Self::dfs_helper(
90+
l_node.borrow().left.clone(),
91+
l_node.borrow().right.clone(),
92+
level + 1,
93+
);
94+
}
95+
(Some(l_node), Some(r_node)) => {
96+
if rev {
97+
let lval = l_node.borrow().val;
98+
let rval = r_node.borrow().val;
99+
l_node.borrow_mut().val = rval;
100+
r_node.borrow_mut().val = lval;
101+
}
102+
103+
Self::dfs_helper(
104+
l_node.borrow().left.clone(),
105+
r_node.borrow().right.clone(),
106+
level + 1,
107+
);
108+
Self::dfs_helper(
109+
l_node.borrow().right.clone(),
110+
r_node.borrow().left.clone(),
111+
level + 1,
112+
);
113+
}
114+
(None, Some(r_node)) => {
115+
Self::dfs_helper(
116+
r_node.borrow().left.clone(),
117+
r_node.borrow().right.clone(),
118+
level + 1,
119+
);
120+
}
121+
_ => {}
122+
}
123+
}
124+
}
125+
126+
// submission codes end
127+
128+
#[cfg(test)]
129+
mod tests {
130+
use super::*;
131+
132+
#[test]
133+
fn test_2415_example_1() {
134+
let root = tree![2, 3, 5, 8, 13, 21, 34];
135+
136+
let result = tree![2, 5, 3, 8, 13, 21, 34];
137+
138+
assert_eq!(Solution::reverse_odd_levels(root), result);
139+
}
140+
141+
#[test]
142+
fn test_2415_example_2() {
143+
let root = tree![7, 13, 11];
144+
145+
let result = tree![7, 11, 13];
146+
147+
assert_eq!(Solution::reverse_odd_levels(root), result);
148+
}
149+
150+
#[test]
151+
fn test_2415_example_3() {
152+
let root = tree![0, 1, 2, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2];
153+
154+
let result = tree![0, 2, 1, 0, 0, 0, 0, 2, 2, 2, 2, 1, 1, 1, 1];
155+
156+
assert_eq!(Solution::reverse_odd_levels(root), result);
157+
}
158+
}

0 commit comments

Comments
 (0)