Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e0f93e8

Browse files
committed
feat: add solutions to lc problem: No.0104
No.0104.Maximum Depth of Binary Tree
1 parent 0b0b9a5 commit e0f93e8

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-0
lines changed

‎solution/0100-0199/0104.Maximum Depth of Binary Tree/README.md‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,69 @@ var maxDepth = function (root) {
156156
};
157157
```
158158

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+
159222
### **...**
160223

161224
```

‎solution/0100-0199/0104.Maximum Depth of Binary Tree/README_EN.md‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,69 @@ var maxDepth = function (root) {
156156
};
157157
```
158158

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+
159222
### **...**
160223

161224
```
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+
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+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /