Skip to content

Commit 1dd4728

Browse files
committed
Add solution and test-cases for problem 559
1 parent 09a1dcb commit 1dd4728

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# [559.Maximum Depth of N-ary Tree][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given a n-ary tree, find its maximum depth.
5+
6+
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
7+
8+
Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).
79

8-
**Example 1:**
10+
**Example 1:**
11+
12+
![1](./narytreeexample.png)
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: root = [1,null,3,2,4,null,5,6]
16+
Output: 3
1317
```
1418

15-
## 题意
16-
> ...
19+
**Example 2:**
1720

18-
## 题解
21+
![2](./sample_4_964.png)
1922

20-
### 思路1
21-
> ...
22-
Maximum Depth of N-ary Tree
23-
```go
2423
```
25-
24+
Input: root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
25+
Output: 5
26+
```
2627

2728
## 结语
2829

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type Node struct {
4+
Val int
5+
Children []*Node
6+
}
7+
8+
func Solution(root *Node) int {
9+
if root == nil {
10+
return 0
11+
}
12+
dep := 0
13+
for _, c := range root.Children {
14+
dep = max(dep, Solution(c))
15+
}
16+
return dep + 1
517
}

leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/Solution_test.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,26 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs *Node
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", &Node{
17+
Val: 1,
18+
Children: []*Node{
19+
{Val: 3, Children: []*Node{{Val: 5}, {Val: 6}}}, {Val: 2}, {Val: 4},
20+
},
21+
}, 3},
22+
{"TestCase2", &Node{
23+
Val: 1,
24+
Children: []*Node{
25+
{Val: 2},
26+
{Val: 3, Children: []*Node{{Val: 6}, {
27+
Val: 7, Children: []*Node{{Val: 11, Children: []*Node{{Val: 14}}}},
28+
}}},
29+
{Val: 4, Children: []*Node{{Val: 8, Children: []*Node{{Val: 12}}}}},
30+
{Val: 5, Children: []*Node{{Val: 9, Children: []*Node{{Val: 13}}}, {Val: 10}}},
31+
},
32+
}, 5},
1933
}
2034

2135
// 开始测试
@@ -30,10 +44,10 @@ func TestSolution(t *testing.T) {
3044
}
3145
}
3246

33-
// 压力测试
47+
// 压力测试
3448
func BenchmarkSolution(b *testing.B) {
3549
}
3650

37-
// 使用案列
51+
// 使用案列
3852
func ExampleSolution() {
3953
}
Loading
Loading

0 commit comments

Comments
 (0)