Skip to content

Commit

Permalink
feat: add solutions to lc problems: No.0559,0563 (#3948)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme authored Jan 13, 2025
1 parent 8b09d75 commit 355400e
Show file tree
Hide file tree
Showing 17 changed files with 377 additions and 186 deletions.
63 changes: 49 additions & 14 deletions solution/0500-0599/0559.Maximum Depth of N-ary Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:递归

我们首先判断 $\textit{root}$ 是否为空,若为空则返回 0。否则我们初始化一个变量 $\textit{mx}$ 用来记录子节点的最大深度,然后遍历 $\textit{root}$ 的所有子节点,递归调用 $\text{maxDepth}$ 函数,更新 $\textit{mx}$ 的值。最后返回 $\textit{mx} + 1$ 即可。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点的数量。

<!-- tabs:start -->

Expand All @@ -69,17 +73,20 @@ tags:
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None):
self.val = val
self.children = children
"""


class Solution:
def maxDepth(self, root: 'Node') -> int:
def maxDepth(self, root: "Node") -> int:
if root is None:
return 0
return 1 + max([self.maxDepth(child) for child in root.children], default=0)
mx = 0
for child in root.children:
mx = max(mx, self.maxDepth(child))
return 1 + mx
```

#### Java
Expand Down Expand Up @@ -109,11 +116,11 @@ class Solution {
if (root == null) {
return 0;
}
int ans = 1;
int mx = 0;
for (Node child : root.children) {
ans = Math.max(ans, 1 + maxDepth(child));
mx = Math.max(mx, maxDepth(child));
}
return ans;
return 1 + mx;
}
}
```
Expand Down Expand Up @@ -144,10 +151,14 @@ public:
class Solution {
public:
int maxDepth(Node* root) {
if (!root) return 0;
int ans = 1;
for (auto& child : root->children) ans = max(ans, 1 + maxDepth(child));
return ans;
if (!root) {
return 0;
}
int mx = 0;
for (Node* child : root->children) {
mx = max(mx, maxDepth(child));
}
return mx + 1;
}
};
```
Expand All @@ -167,11 +178,35 @@ func maxDepth(root *Node) int {
if root == nil {
return 0
}
ans := 1
mx := 0
for _, child := range root.Children {
ans = max(ans, 1+maxDepth(child))
mx = max(mx, maxDepth(child))
}
return ans
return 1 + mx
}
```

#### TypeScript

```ts
/**
* Definition for _Node.
* class _Node {
* val: number
* children: _Node[]
*
* constructor(val?: number, children?: _Node[]) {
* this.val = (val===undefined ? 0 : val)
* this.children = (children===undefined ? [] : children)
* }
* }
*/

function maxDepth(root: _Node | null): number {
if (!root) {
return 0;
}
return 1 + Math.max(...root.children.map(child => maxDepth(child)), 0);
}
```

Expand Down
63 changes: 49 additions & 14 deletions solution/0500-0599/0559.Maximum Depth of N-ary Tree/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Recursion

First, we check if $\textit{root}$ is null. If it is, we return 0. Otherwise, we initialize a variable $\textit{mx}$ to record the maximum depth of the child nodes, then traverse all the child nodes of $\textit{root}$, recursively call the $\text{maxDepth}$ function, and update the value of $\textit{mx}$. Finally, we return $\textit{mx} + 1$.

The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes.

<!-- tabs:start -->

Expand All @@ -67,17 +71,20 @@ tags:
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None):
self.val = val
self.children = children
"""


class Solution:
def maxDepth(self, root: 'Node') -> int:
def maxDepth(self, root: "Node") -> int:
if root is None:
return 0
return 1 + max([self.maxDepth(child) for child in root.children], default=0)
mx = 0
for child in root.children:
mx = max(mx, self.maxDepth(child))
return 1 + mx
```

#### Java
Expand Down Expand Up @@ -107,11 +114,11 @@ class Solution {
if (root == null) {
return 0;
}
int ans = 1;
int mx = 0;
for (Node child : root.children) {
ans = Math.max(ans, 1 + maxDepth(child));
mx = Math.max(mx, maxDepth(child));
}
return ans;
return 1 + mx;
}
}
```
Expand Down Expand Up @@ -142,10 +149,14 @@ public:
class Solution {
public:
int maxDepth(Node* root) {
if (!root) return 0;
int ans = 1;
for (auto& child : root->children) ans = max(ans, 1 + maxDepth(child));
return ans;
if (!root) {
return 0;
}
int mx = 0;
for (Node* child : root->children) {
mx = max(mx, maxDepth(child));
}
return mx + 1;
}
};
```
Expand All @@ -165,11 +176,35 @@ func maxDepth(root *Node) int {
if root == nil {
return 0
}
ans := 1
mx := 0
for _, child := range root.Children {
ans = max(ans, 1+maxDepth(child))
mx = max(mx, maxDepth(child))
}
return ans
return 1 + mx
}
```

#### TypeScript

```ts
/**
* Definition for _Node.
* class _Node {
* val: number
* children: _Node[]
*
* constructor(val?: number, children?: _Node[]) {
* this.val = (val===undefined ? 0 : val)
* this.children = (children===undefined ? [] : children)
* }
* }
*/

function maxDepth(root: _Node | null): number {
if (!root) {
return 0;
}
return 1 + Math.max(...root.children.map(child => maxDepth(child)), 0);
}
```

Expand Down
14 changes: 9 additions & 5 deletions solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ class Node {
class Solution {
public:
int maxDepth(Node* root) {
if (!root) return 0;
int ans = 1;
for (auto& child : root->children) ans = max(ans, 1 + maxDepth(child));
return ans;
if (!root) {
return 0;
}
int mx = 0;
for (Node* child : root->children) {
mx = max(mx, maxDepth(child));
}
return mx + 1;
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ func maxDepth(root *Node) int {
if root == nil {
return 0
}
ans := 1
mx := 0
for _, child := range root.Children {
ans = max(ans, 1+maxDepth(child))
mx = max(mx, maxDepth(child))
}
return ans
}
return 1 + mx
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public int maxDepth(Node root) {
if (root == null) {
return 0;
}
int ans = 1;
int mx = 0;
for (Node child : root.children) {
ans = Math.max(ans, 1 + maxDepth(child));
mx = Math.max(mx, maxDepth(child));
}
return ans;
return 1 + mx;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
"""
# Definition for a Node.
class Node:
def __init__(self, val=None, children=None):
def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None):
self.val = val
self.children = children
"""


class Solution:
def maxDepth(self, root: 'Node') -> int:
def maxDepth(self, root: "Node") -> int:
if root is None:
return 0
return 1 + max([self.maxDepth(child) for child in root.children], default=0)
mx = 0
for child in root.children:
mx = max(mx, self.maxDepth(child))
return 1 + mx
19 changes: 19 additions & 0 deletions solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Definition for _Node.
* class _Node {
* val: number
* children: _Node[]
*
* constructor(val?: number, children?: _Node[]) {
* this.val = (val===undefined ? 0 : val)
* this.children = (children===undefined ? [] : children)
* }
* }
*/

function maxDepth(root: _Node | null): number {
if (!root) {
return 0;
}
return 1 + Math.max(...root.children.map(child => maxDepth(child)), 0);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ tags:
遍历矩阵,当遇到 $1$ 时,更新 $f[i][j][k]$ 的值。对于每个位置 $(i, j)$,我们只需要更新其四个方向的值即可。然后更新答案。

时间复杂度 $O(m\times n)$,空间复杂度 $O(m\times n)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。
时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。

<!-- tabs:start -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

We define $f[i][j][k]$ to represent the length of the longest consecutive $1$s ending at $(i, j)$ in direction $k$. The value range of $k$ is $0, 1, 2, 3$, representing horizontal, vertical, diagonal, and anti-diagonal directions, respectively.

> We can also use four 2D arrays to represent the length of the longest consecutive $1$s in the four directions.
We traverse the matrix, and when we encounter $1$, we update the value of $f[i][j][k]$. For each position $(i, j)$, we only need to update the values in its four directions. Then we update the answer.

The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns in the matrix, respectively.

<!-- tabs:start -->

Expand Down
Loading

0 comments on commit 355400e

Please sign in to comment.