diff --git a/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/README.md b/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/README.md index fcbaab6ae..17efe5680 100644 --- a/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/README.md +++ b/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/Solution.go b/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/Solution.go index d115ccf5e..cbdbdac80 100644 --- a/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/Solution.go +++ b/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/Solution.go @@ -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 } diff --git a/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/Solution_test.go b/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/Solution_test.go index 14ff50eb4..939c2f297 100644 --- a/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/Solution_test.go +++ b/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +44,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/narytreeexample.png b/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/narytreeexample.png new file mode 100644 index 000000000..d28c543c8 Binary files /dev/null and b/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/narytreeexample.png differ diff --git a/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/sample_4_964.png b/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/sample_4_964.png new file mode 100644 index 000000000..f4347c766 Binary files /dev/null and b/leetcode/501-600/0559.Maximum-Depth-of-N-ary-Tree/sample_4_964.png differ