力扣刷题
给定一个 N 叉树,返回其节点值的后序遍历。
例如,给定一个 3叉树 :
解题思路:递归
/**
* // Definition for a Node.
* function Node(val,children) {
* this.val = val;
* this.children = children;
* };
*/
/**
* @param {Node} root
* @return {number[]}
*/
var postorder = function(root) {
const res = []
function hx (root) {
if (root !== null) {
root.children.forEach(child => {
hx(child)
})
res.push(root.val)
}
}
hx(root)
return res
};