Skip to content

Leetcode 75 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 59 commits into from
Apr 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
8c191cd
Update 394. Decode String.py
iamAntimPal Apr 13, 2025
6a252d4
Create readme.md
iamAntimPal Apr 13, 2025
b11280a
Create 933. Number of Recent Calls.py
iamAntimPal Apr 13, 2025
144314a
Update 933. Number of Recent Calls.py
iamAntimPal Apr 13, 2025
120ce7d
Create readme.md
iamAntimPal Apr 13, 2025
9d52964
Update readme.md
iamAntimPal Apr 13, 2025
4b23a61
Create 649. Dota2 Senate.py
iamAntimPal Apr 13, 2025
71b20b7
Create readme.md
iamAntimPal Apr 13, 2025
b49235e
Update readme.md
iamAntimPal Apr 13, 2025
a4ca44c
Create 2095. Delete the Middle Node of a Linked List.py
iamAntimPal Apr 13, 2025
8f22d0f
Update 2095. Delete the Middle Node of a Linked List.py
iamAntimPal Apr 13, 2025
d8b9da9
Update 2095. Delete the Middle Node of a Linked List.py
iamAntimPal Apr 13, 2025
644e994
Create readme.md
iamAntimPal Apr 13, 2025
88760de
Update readme.md
iamAntimPal Apr 13, 2025
471ce9f
Update readme.md
iamAntimPal Apr 13, 2025
75e6293
Create 328. Odd Even Linked List.py
iamAntimPal Apr 13, 2025
7c271bb
Create readme.md
iamAntimPal Apr 13, 2025
a8a6716
Update readme.md
iamAntimPal Apr 13, 2025
46d6c5a
Create 206. Reverse Linked List.py
iamAntimPal Apr 13, 2025
822e01e
Create readme.md
iamAntimPal Apr 13, 2025
bfce5eb
Update readme.md
iamAntimPal Apr 13, 2025
f2c1f52
Create 2130. Maximum Twin Sum of a Linked List.py
iamAntimPal Apr 13, 2025
0cdf1a9
code
iamAntimPal Apr 13, 2025
ca2464e
Create readme.md
iamAntimPal Apr 13, 2025
660436a
Update readme.md
iamAntimPal Apr 13, 2025
942e12c
Update readme.md
iamAntimPal Apr 13, 2025
9664552
Update readme.md
iamAntimPal Apr 13, 2025
153dfe5
Create 104. Maximum Depth of Binary Tree.py
iamAntimPal Apr 13, 2025
37aad9a
Update 104. Maximum Depth of Binary Tree.py
iamAntimPal Apr 13, 2025
d2beb06
Create readme.md
iamAntimPal Apr 13, 2025
4a4be27
Update readme.md
iamAntimPal Apr 13, 2025
b8de5a1
Update readme.md
iamAntimPal Apr 13, 2025
d57461b
Create 872. Leaf-Similar Trees.py
iamAntimPal Apr 13, 2025
f9a0f48
Update 872. Leaf-Similar Trees.py
iamAntimPal Apr 13, 2025
8214da8
Create readme.md
iamAntimPal Apr 13, 2025
7a0c057
Update readme.md
iamAntimPal Apr 13, 2025
127fe4f
Update readme.md
iamAntimPal Apr 13, 2025
4e61cf1
Create 1448. Count Good Nodes in Binary Tree.py
iamAntimPal Apr 13, 2025
5fc0e7a
Update 1448. Count Good Nodes in Binary Tree.py
iamAntimPal Apr 13, 2025
51cb612
Create readme.md
iamAntimPal Apr 13, 2025
3992394
Update readme.md
iamAntimPal Apr 13, 2025
1b86445
Update readme.md
iamAntimPal Apr 13, 2025
85eba4e
Update readme.md
iamAntimPal Apr 13, 2025
144b3f2
Create 437. Path Sum III.py
iamAntimPal Apr 13, 2025
9fc52c4
Update 437. Path Sum III.py
iamAntimPal Apr 13, 2025
ae5091a
Create readme.md
iamAntimPal Apr 13, 2025
27355f6
Update readme.md
iamAntimPal Apr 13, 2025
c329827
Create 1372. Longest ZigZag Path in a Binary Tree.py
iamAntimPal Apr 13, 2025
df54adb
Create readme.md
iamAntimPal Apr 13, 2025
5326bdb
Update readme.md
iamAntimPal Apr 13, 2025
6a1ab0e
Update readme.md
iamAntimPal Apr 13, 2025
9ddfb3e
Create 236. Lowest Common Ancestor of a Binary Tree.py
iamAntimPal Apr 13, 2025
943b2a1
Update 236. Lowest Common Ancestor of a Binary Tree.py
iamAntimPal Apr 13, 2025
d95eaec
Create readme.md
iamAntimPal Apr 13, 2025
8877714
Update readme.md
iamAntimPal Apr 13, 2025
71463bd
Update readme.md
iamAntimPal Apr 13, 2025
5d1df5d
Update readme.md
iamAntimPal Apr 13, 2025
a46ebde
Create 199. Binary Tree Right Side View.py
iamAntimPal Apr 13, 2025
b2760cc
Create readme.md
iamAntimPal Apr 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if root is None:
return 0
l, r = self.maxDepth(root.left), self.maxDepth(root.right)
return 1 + max(l, r)
268 changes: 268 additions & 0 deletions Solution/104. Maximum Depth of Binary Tree/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@

<!-- problem:start -->

# [104. Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree)

---
- **comments**: true
- **difficulty**: Easy
- **edit_url**: https://github.com/doocs/leetcode/edit/main/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md
- **tags**:
- Tree
- Depth-First Search
- Breadth-First Search
- Binary Tree
---


## Description

<!-- description:start -->

<p>Given the <code>root</code> of a binary tree, return <em>its maximum depth</em>.</p>

<p>A binary tree&#39;s <strong>maximum depth</strong>&nbsp;is the number of nodes along the longest path from the root node down to the farthest leaf node.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/images/tmp-tree.jpg" style="width: 400px; height: 277px;" />
<pre>
<strong>Input:</strong> root = [3,9,20,null,null,15,7]
<strong>Output:</strong> 3
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> root = [1,null,2]
<strong>Output:</strong> 2
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
<li>The number of nodes in the tree is in the range <code>[0, 10<sup>4</sup>]</code>.</li>
<li><code>-100 &lt;= Node.val &lt;= 100</code></li>
</ul>

<!-- description:end -->

## Solutions

<!-- solution:start -->

### Solution 1: Recursion

Recursively traverse the left and right subtrees, calculate the maximum depth of the left and right subtrees, and then take the maximum value plus $1$.

The time complexity is $O(n)$, where $n$ is the number of nodes in the binary tree. Each node is traversed only once in the recursion.

<!-- tabs:start -->

#### Python3

```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: TreeNode) -> int:
if root is None:
return 0
l, r = self.maxDepth(root.left), self.maxDepth(root.right)
return 1 + max(l, r)
```

#### Java

```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
int l = maxDepth(root.left);
int r = maxDepth(root.right);
return 1 + Math.max(l, r);
}
}
```

#### C++

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if (!root) return 0;
int l = maxDepth(root->left), r = maxDepth(root->right);
return 1 + max(l, r);
}
};
```

#### Go

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func maxDepth(root *TreeNode) int {
if root == nil {
return 0
}
l, r := maxDepth(root.Left), maxDepth(root.Right)
return 1 + max(l, r)
}
```

#### TypeScript

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function maxDepth(root: TreeNode | null): number {
if (root === null) {
return 0;
}
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
```

#### Rust

```rust
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>) -> i32 {
if root.is_none() {
return 0;
}
let node = root.as_ref().unwrap().borrow();
1 + Self::dfs(&node.left).max(Self::dfs(&node.right))
}

pub fn max_depth(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
Self::dfs(&root)
}
}
```

#### JavaScript

```js
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function (root) {
if (!root) return 0;
const l = maxDepth(root.left);
const r = maxDepth(root.right);
return 1 + Math.max(l, r);
};
```

#### C

```c
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/

#define max(a, b) (((a) > (b)) ? (a) : (b))

int maxDepth(struct TreeNode* root) {
if (!root) {
return 0;
}
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return 1 + max(left, right);
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def longestZigZag(self, root: TreeNode) -> int:
def dfs(root, l, r):
if root is None:
return
nonlocal ans
ans = max(ans, l, r)
dfs(root.left, r + 1, 0)
dfs(root.right, 0, l + 1)

ans = 0
dfs(root, 0, 0)
return ans
Loading