-
Notifications
You must be signed in to change notification settings - Fork 0
/
diameter_of_binary_tree.rs
85 lines (71 loc) · 2.12 KB
/
diameter_of_binary_tree.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#![allow(dead_code)]
use std::cell::RefCell;
use std::rc::Rc;
type Tree = Option<Rc<RefCell<TreeNode>>>;
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Tree,
pub right: Tree,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}
pub fn diameter_of_binary_tree(root: Tree) -> i32 {
let mut max = 0;
fn helper(root: Tree, max: &mut i32) -> i32 {
if let Some(node) = root {
let left = helper(node.borrow().left.clone(), max);
let right = helper(node.borrow().right.clone(), max);
*max = std::cmp::max(*max, left + right);
std::cmp::max(left, right) + 1
} else {
0
}
}
helper(root, &mut max);
max
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
let mut root = TreeNode::new(1);
let mut left = TreeNode::new(2);
let right = TreeNode::new(3);
let left_left = TreeNode::new(4);
let left_right = TreeNode::new(5);
left.left = Some(Rc::new(RefCell::new(left_left)));
left.right = Some(Rc::new(RefCell::new(left_right)));
root.left = Some(Rc::new(RefCell::new(left)));
root.right = Some(Rc::new(RefCell::new(right)));
assert_eq!(
diameter_of_binary_tree(Some(Rc::new(RefCell::new(root)))),
3
);
}
#[test]
fn test_2() {
let mut root = TreeNode::new(1);
let mut left = TreeNode::new(2);
let mut right = TreeNode::new(3);
left.left = Some(Rc::new(RefCell::new(TreeNode::new(4))));
left.right = Some(Rc::new(RefCell::new(TreeNode::new(5))));
right.left = Some(Rc::new(RefCell::new(TreeNode::new(6))));
right.right = Some(Rc::new(RefCell::new(TreeNode::new(7))));
root.left = Some(Rc::new(RefCell::new(left)));
root.right = Some(Rc::new(RefCell::new(right)));
assert_eq!(
diameter_of_binary_tree(Some(Rc::new(RefCell::new(root)))),
4
);
}
}