From d4704811b9bd21dc6341a08a6f4bc19db0703027 Mon Sep 17 00:00:00 2001 From: tphyhFighting <2363176358@qq.com> Date: Sun, 21 Nov 2021 12:01:24 +0800 Subject: [PATCH] add: leetcode 0559 solution --- .../559.Maximum Depth of N-ary Tree.go | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree.go diff --git a/leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree.go b/leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree.go new file mode 100644 index 00000000..30fbc943 --- /dev/null +++ b/leetcode/0559.Maximum-Depth-of-N-ary-Tree/559.Maximum Depth of N-ary Tree.go @@ -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 +}