Back to all solutions
#589 - N-ary Tree Preorder Traversal
Problem Description
Given the root of an n-ary tree, return the preorder traversal of its nodes' values.
Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples)
Solution
/**
* // Definition for a _Node.
* function _Node(val, children) {
* this.val = val;
* this.children = children;
* };
*/
/**
* @param {_Node|null} root
* @return {number[]}
*/
var preorder = function(root) {
return !root ? [] : [root.val, ...root.children.flatMap(n => preorder(n))];
};