Skip to content

Commit e7f47fc

Browse files
committed
feat: add rust solution to lcof problem: No.55
面试题55 - I. 二叉树的深度
1 parent a13f89e commit e7f47fc

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

lcof/面试题55 - I. 二叉树的深度/README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,44 @@ func maxDepth(root *TreeNode) int {
137137
}
138138
```
139139

140+
### **Rust**
141+
142+
```rust
143+
// Definition for a binary tree node.
144+
// #[derive(Debug, PartialEq, Eq)]
145+
// pub struct TreeNode {
146+
// pub val: i32,
147+
// pub left: Option<Rc<RefCell<TreeNode>>>,
148+
// pub right: Option<Rc<RefCell<TreeNode>>>,
149+
// }
150+
//
151+
// impl TreeNode {
152+
// #[inline]
153+
// pub fn new(val: i32) -> Self {
154+
// TreeNode {
155+
// val,
156+
// left: None,
157+
// right: None
158+
// }
159+
// }
160+
// }
161+
use std::rc::Rc;
162+
use std::cell::RefCell;
163+
impl Solution {
164+
pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
165+
match root {
166+
None => 0,
167+
Some(node) => {
168+
let mut node = node.borrow_mut();
169+
let left = node.left.take();
170+
let right = node.right.take();
171+
1 + Self::max_depth(left).max(Self::max_depth(right))
172+
}
173+
}
174+
}
175+
}
176+
```
177+
140178
### **...**
141179

142180
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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(node) => {
26+
let mut node = node.borrow_mut();
27+
let left = node.left.take();
28+
let right = node.right.take();
29+
1 + Self::max_depth(left).max(Self::max_depth(right))
30+
}
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)