File tree Expand file tree Collapse file tree 4 files changed +179
-0
lines changed
solution/0100-0199/0104.Maximum Depth of Binary Tree Expand file tree Collapse file tree 4 files changed +179
-0
lines changed Original file line number Diff line number Diff line change @@ -156,6 +156,69 @@ var maxDepth = function (root) {
156
156
};
157
157
```
158
158
159
+ ### ** TypeScript**
160
+
161
+ ``` ts
162
+ /**
163
+ * Definition for a binary tree node.
164
+ * class TreeNode {
165
+ * val: number
166
+ * left: TreeNode | null
167
+ * right: TreeNode | null
168
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
169
+ * this.val = (val===undefined ? 0 : val)
170
+ * this.left = (left===undefined ? null : left)
171
+ * this.right = (right===undefined ? null : right)
172
+ * }
173
+ * }
174
+ */
175
+
176
+ function maxDepth(root : TreeNode | null ): number {
177
+ if (root == null ) {
178
+ return 0 ;
179
+ }
180
+ return 1 + Math .max (maxDepth (root .left ), maxDepth (root .right ));
181
+ }
182
+ ```
183
+
184
+ ### ** Rust**
185
+
186
+ ``` rust
187
+ // Definition for a binary tree node.
188
+ // #[derive(Debug, PartialEq, Eq)]
189
+ // pub struct TreeNode {
190
+ // pub val: i32,
191
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
192
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
193
+ // }
194
+ //
195
+ // impl TreeNode {
196
+ // #[inline]
197
+ // pub fn new(val: i32) -> Self {
198
+ // TreeNode {
199
+ // val,
200
+ // left: None,
201
+ // right: None
202
+ // }
203
+ // }
204
+ // }
205
+ use std :: rc :: Rc ;
206
+ use std :: cell :: RefCell ;
207
+ impl Solution {
208
+ fn dfs (root : & Option <Rc <RefCell <TreeNode >>>) -> i32 {
209
+ if root . is_none () {
210
+ return 0 ;
211
+ }
212
+ let node = root . as_ref (). unwrap (). borrow ();
213
+ 1 + Self :: dfs (& node . left). max (Self :: dfs (& node . right))
214
+ }
215
+
216
+ pub fn max_depth (root : Option <Rc <RefCell <TreeNode >>>) -> i32 {
217
+ Self :: dfs (& root )
218
+ }
219
+ }
220
+ ```
221
+
159
222
### ** ...**
160
223
161
224
```
Original file line number Diff line number Diff line change @@ -156,6 +156,69 @@ var maxDepth = function (root) {
156
156
};
157
157
```
158
158
159
+ ### ** TypeScript**
160
+
161
+ ``` ts
162
+ /**
163
+ * Definition for a binary tree node.
164
+ * class TreeNode {
165
+ * val: number
166
+ * left: TreeNode | null
167
+ * right: TreeNode | null
168
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
169
+ * this.val = (val===undefined ? 0 : val)
170
+ * this.left = (left===undefined ? null : left)
171
+ * this.right = (right===undefined ? null : right)
172
+ * }
173
+ * }
174
+ */
175
+
176
+ function maxDepth(root : TreeNode | null ): number {
177
+ if (root == null ) {
178
+ return 0 ;
179
+ }
180
+ return 1 + Math .max (maxDepth (root .left ), maxDepth (root .right ));
181
+ }
182
+ ```
183
+
184
+ ### ** Rust**
185
+
186
+ ``` rust
187
+ // Definition for a binary tree node.
188
+ // #[derive(Debug, PartialEq, Eq)]
189
+ // pub struct TreeNode {
190
+ // pub val: i32,
191
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
192
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
193
+ // }
194
+ //
195
+ // impl TreeNode {
196
+ // #[inline]
197
+ // pub fn new(val: i32) -> Self {
198
+ // TreeNode {
199
+ // val,
200
+ // left: None,
201
+ // right: None
202
+ // }
203
+ // }
204
+ // }
205
+ use std :: rc :: Rc ;
206
+ use std :: cell :: RefCell ;
207
+ impl Solution {
208
+ fn dfs (root : & Option <Rc <RefCell <TreeNode >>>) -> i32 {
209
+ if root . is_none () {
210
+ return 0 ;
211
+ }
212
+ let node = root . as_ref (). unwrap (). borrow ();
213
+ 1 + Self :: dfs (& node . left). max (Self :: dfs (& node . right))
214
+ }
215
+
216
+ pub fn max_depth (root : Option <Rc <RefCell <TreeNode >>>) -> i32 {
217
+ Self :: dfs (& root )
218
+ }
219
+ }
220
+ ```
221
+
159
222
### ** ...**
160
223
161
224
```
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
+ fn dfs ( root : & Option < Rc < RefCell < TreeNode > > > ) -> i32 {
23
+ if root. is_none ( ) {
24
+ return 0 ;
25
+ }
26
+ let node = root. as_ref ( ) . unwrap ( ) . borrow ( ) ;
27
+ 1 + Self :: dfs ( & node. left ) . max ( Self :: dfs ( & node. right ) )
28
+ }
29
+
30
+ pub fn max_depth ( root : Option < Rc < RefCell < TreeNode > > > ) -> i32 {
31
+ Self :: dfs ( & root)
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * class TreeNode {
4
+ * val: number
5
+ * left: TreeNode | null
6
+ * right: TreeNode | null
7
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8
+ * this.val = (val===undefined ? 0 : val)
9
+ * this.left = (left===undefined ? null : left)
10
+ * this.right = (right===undefined ? null : right)
11
+ * }
12
+ * }
13
+ */
14
+
15
+ function maxDepth ( root : TreeNode | null ) : number {
16
+ if ( root == null ) {
17
+ return 0 ;
18
+ }
19
+ return 1 + Math . max ( maxDepth ( root . left ) , maxDepth ( root . right ) ) ;
20
+ }
You can’t perform that action at this time.
0 commit comments