The title

Given an n-tree, returns a sequential traversal of its node values.

The n-fork tree is serialized in the input as a sequential traversal, with each set of child nodes separated by null values (see example).

Example 1:

Input: root = [1, null, 3 4-trichlorobenzene, null, 5, 6] output:,3,5,6,2,4 [1]Copy the code

Their thinking

  1. The preceding traversal of a binary tree is root – left subtree – subtree
  2. This problem is an n-tree, there are no left and right subtrees, only an array of child nodes
  3. N fork tree traversal: root-subarray

Code implementation

var preorder = function(root) { if (! root) return [] const ans = [root.val] function traversal(root) { for(let child of root.children) { ans.push(child.val) traversal(child) } } traversal(root) return ans };Copy the code

If there are mistakes welcome to point out, welcome to discuss together!