Skip to content

Commit 8c9f7df

Browse files
authored
maximum depth of binary tree solution
1 parent 65f77b6 commit 8c9f7df

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
23+
match root {
24+
None => 0,
25+
Some(u) => {
26+
let u = u.borrow();
27+
1 + Self::max_depth(u.left.clone()).max(Self::max_depth(u.right.clone()))
28+
}
29+
}
30+
}
31+
}

0 commit comments

Comments
 (0)