LouisTsang-jk.github.io

N 叉树的后序遍历

出处

leetcode | N叉树的后序遍历

难度: 简单

题解/思路

递归

/**
* // Definition for a Node.
* function Node(val,children) {
*    this.val = val;
*    this.children = children;
* };
*/

/**
* @param {Node|null} root
* @return {number[]}
*/
var postorder = function (root) {
if (!root) return []
const res = [];
function dfs(node) {
  if (!node) return;
  for (let i = 0; i < node.children.length; i++) {
    dfs(node.children[i]);
  }
  res.push(node.val);
}
dfs(root);
return res;
};