559. Maximum Depth of N-ary Tree
Golang struct
/**
* Definition for a Node.
* type Node struct {
* Val int
* Children []*Node
* }
*/
Solution 1
func maxDepth(root *Node) int {
if root == nil {
return 0
}
maxDepth := 1
Recursion(root, &maxDepth, 1)
return maxDepth
}
func Recursion(cur *Node, maxDepth *int, depth int) {
*maxDepth = max(depth, *maxDepth)
for i := 0; i < len(cur.Children); i++ {
Recursion(cur.Children[i], maxDepth, depth+1)
}
}
func max(i, j int) int {
if i > j {
return i
}
return j
}
Solution 2
func maxDepth(root *Node) int {
if root == nil {
return 0
}
maxDepth := 1
Recursion(root, &maxDepth, 1)
return maxDepth
}
func max(i, j int) int {
if i > j {
return i
}
return j
}