Problem List

N-ary Tree Preorder Traversal

May 30, 2025Go stack, tree, depth first searcheasy

Problem

Reflections

Memorized at this point.

Performance

Complexity

Go Solution
func preorder(root * Node)[]int {
  var res []int
  var dfs func(node * Node)
  dfs = func(node * Node) {
    if node == nil {
      return
    }

    res = append(res, node.Val)
    for _, child := range(node.Children) {
      dfs(child)
    }
  }

  dfs(root)
  return res
}
LeetCode Problem Link