mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 18:10:29 +08:00
add: leetcode 0559 solution
This commit is contained in:
@ -0,0 +1,32 @@
|
|||||||
|
package leetcode
|
||||||
|
|
||||||
|
type Node struct {
|
||||||
|
Val int
|
||||||
|
Children []*Node
|
||||||
|
}
|
||||||
|
|
||||||
|
func maxDepth(root *Node) int {
|
||||||
|
if root == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return 1 + bfs(root)
|
||||||
|
}
|
||||||
|
|
||||||
|
func bfs(root *Node) int {
|
||||||
|
var q []*Node
|
||||||
|
var depth int
|
||||||
|
q = append(q, root.Children...)
|
||||||
|
for len(q) != 0 {
|
||||||
|
depth++
|
||||||
|
length := len(q)
|
||||||
|
for length != 0 {
|
||||||
|
ele := q[0]
|
||||||
|
q = q[1:]
|
||||||
|
length--
|
||||||
|
if ele != nil && len(ele.Children) != 0 {
|
||||||
|
q = append(q, ele.Children...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return depth
|
||||||
|
}
|
Reference in New Issue
Block a user