Dashboard Temp Share Shortlinks Frames API

HTMLify

LeetCode - N-ary Tree Preorder Traversal - Go
Views: 356 | Author: abh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/**
 * Definition for a Node.
 * type Node struct {
 *     Val int
 *     Children []*Node
 * }
 */

func preorder(root *Node) []int {
	var stack []int
	if root == nil {
		return stack
	}
	stack = append(stack, root.Val)
	for _, child := range root.Children {
		stack = append(stack, preorder(child)...)
	}
	return stack
}