I am sorry to say that, but the example solution in Go isn’t really close to any guidelines accepted in said language and thus not really readable without frustration. Here is my solution
var res []string
func dfs(node Node, path []string){
if len(node.children) == 0 {
path = append(path, strconv.Itoa(node.val))
res = append(res, strings.Join(path, "->"))
}
path = append(path, strconv.Itoa(node.val))
for _, c := range node.children {
dfs(c, path)
}
}
func ternaryTreePaths(root Node) []string {
dfs(root, nil)
return res
}
or without nasty package level variable:
func dfs(node Node, path []string) []string{
var res []string
if len(node.children) == 0 {
path = append(path, strconv.Itoa(node.val))
return append(res, strings.Join(path, "->"))
}
path = append(path, strconv.Itoa(node.val))
for _, c := range node.children {
res = append(res,dfs(c, path)...)
}
return res
}
func ternaryTreePaths(root Node) []string {
return dfs(root, nil)
}