589. N 叉树的前序遍历
问题描述: 给定一个 n 叉树的根节点 root ,返回 其节点值的 前序遍历 。
n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。(by leetcode 589)
示例 1:
输入: root = [1,null,3,2,4,null,5,6]
输出: [1,3,5,6,2,4]
思路: 递归
/**
*// Definition for a Node.
* function Node(val, children) {
* this.val = val;
* this.children = children;
* };
*/
/**
* @param {Node|null} root
* @return {number[]}
*/
function pre(root,ans){
if(root==null)return;
ans.push(root.val);
if(root.children.length){
for(let i=0;i<root.children.length;i++){
pre(root.children[i],ans)
}
}
return }
var preorder = function(root) {
let ans=[];
pre(root,ans);
return ans
};