Skip to content

Commit d9eb2f0

Browse files
committed
q662
1 parent 5534454 commit d9eb2f0

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

src/tree/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,5 @@ mod q108;
137137
mod q95;
138138
mod q129;
139139
mod q1382;
140-
mod q199;
140+
mod q199;
141+
mod q662;

src/tree/q662.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/// Given a binary tree, write a function to get the maximum width of the given tree. The maximum width of a tree is the maximum width among all levels.
2+
/// The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted into the length calculation.
3+
///
4+
/// It is guaranteed that the answer will in the range of 32-bit signed integer.
5+
///
6+
/// Example 1:
7+
///
8+
/// Input:
9+
///
10+
/// 1
11+
/// / \
12+
/// 3 2
13+
/// / \ \
14+
/// 5 3 9
15+
///
16+
/// Output: 4
17+
/// Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
18+
/// Example 2:
19+
///
20+
/// Input:
21+
///
22+
/// 1
23+
/// /
24+
/// 3
25+
/// / \
26+
/// 5 3
27+
///
28+
/// Output: 2
29+
/// Explanation: The maximum width existing in the third level with the length 2 (5,3).
30+
/// Example 3:
31+
///
32+
/// Input:
33+
///
34+
/// 1
35+
/// / \
36+
/// 3 2
37+
/// /
38+
/// 5
39+
///
40+
/// Output: 2
41+
/// Explanation: The maximum width existing in the second level with the length 2 (3,2).
42+
/// Example 4:
43+
///
44+
/// Input:
45+
///
46+
/// 1
47+
/// / \
48+
/// 3 2
49+
/// / \
50+
/// 5 9
51+
/// / \
52+
/// 6 7
53+
/// Output: 8
54+
/// Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
55+
///  
56+
///
57+
/// Constraints:
58+
///
59+
/// The given binary tree will have between 1 and 3000 nodes.
60+
///
61+
/// 来源:力扣(LeetCode)
62+
/// 链接:https://leetcode-cn.com/problems/maximum-width-of-binary-tree
63+
/// 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
64+
65+
66+
use super::*;
67+
68+
use std::collections::VecDeque;
69+
use std::cmp::max;
70+
71+
struct Solution{}
72+
impl Solution {
73+
pub fn width_of_binary_tree(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
74+
75+
let mut max_width = 0;
76+
let mut current_height = 0;
77+
let mut left = 0;
78+
let mut queue = VecDeque::new();
79+
queue.push_front((root, 0, 0));
80+
81+
while !queue.is_empty() {
82+
match queue.pop_back() {
83+
Some((Some(n), pos, height) ) => {
84+
queue.push_front((RefCell::borrow(&n).left.clone(), pos * 2, height + 1));
85+
queue.push_front((RefCell::borrow(&n).right.clone(), pos * 2 + 1, height + 1));
86+
87+
if current_height != height {
88+
current_height = height;
89+
left = pos
90+
}
91+
92+
max_width = max(max_width, pos-left+1);
93+
},
94+
_ => ()
95+
}
96+
97+
}
98+
99+
max_width
100+
}
101+
}
102+
103+
#[cfg(test)]
104+
mod tests {
105+
use super::*;
106+
107+
#[test]
108+
fn test_1() {
109+
assert_eq!(Solution::width_of_binary_tree(tree!(1,2,3)), 2)
110+
}
111+
}
112+

0 commit comments

Comments
 (0)