Skip to content

Commit

Permalink
Merge pull request #848 from 0xff-dev/559
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 559
  • Loading branch information
6boris authored Jun 6, 2024
2 parents f8c96ee + 1dd4728 commit a52b3ea
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
29 changes: 15 additions & 14 deletions leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
# [559.Maximum Depth of N-ary Tree][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
## Description
Given a n-ary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples).

**Example 1:**
**Example 1:**

![1](./narytreeexample.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: root = [1,null,3,2,4,null,5,6]
Output: 3
```

## 题意
> ...
**Example 2:**

## 题解
![2](./sample_4_964.png)

### 思路1
> ...
Maximum Depth of N-ary Tree
```go
```

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]
Output: 5
```

## 结语

Expand Down
16 changes: 14 additions & 2 deletions leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package Solution

func Solution(x bool) bool {
return x
type Node struct {
Val int
Children []*Node
}

func Solution(root *Node) int {
if root == nil {
return 0
}
dep := 0
for _, c := range root.Children {
dep = max(dep, Solution(c))
}
return dep + 1
}
28 changes: 21 additions & 7 deletions leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,26 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs *Node
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", &Node{
Val: 1,
Children: []*Node{
{Val: 3, Children: []*Node{{Val: 5}, {Val: 6}}}, {Val: 2}, {Val: 4},
},
}, 3},
{"TestCase2", &Node{
Val: 1,
Children: []*Node{
{Val: 2},
{Val: 3, Children: []*Node{{Val: 6}, {
Val: 7, Children: []*Node{{Val: 11, Children: []*Node{{Val: 14}}}},
}}},
{Val: 4, Children: []*Node{{Val: 8, Children: []*Node{{Val: 12}}}}},
{Val: 5, Children: []*Node{{Val: 9, Children: []*Node{{Val: 13}}}, {Val: 10}}},
},
}, 5},
}

// 开始测试
Expand All @@ -30,10 +44,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a52b3ea

Please sign in to comment.