File tree Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Expand file tree Collapse file tree 2 files changed +71
-0
lines changed Original file line number Diff line number Diff line change @@ -137,6 +137,44 @@ func maxDepth(root *TreeNode) int {
137
137
}
138
138
```
139
139
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
+
140
178
### ** ...**
141
179
142
180
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments