题目,589. N-ary Tree Preorder Traversal 解析 n叉树的前序遍历,根->左->右先push根,然后遍历子树,使用前序递归子树。注意数组的拼接。 1234567891011 var preorder = function(root) { const res = []; if(!root) { return []; } res.push(root.val) for(let i=0; i<root.children.length; i++) { res.push(...preorder(root.children[i])); } return res;}; 赞微海报分享
近期评论