145. 二叉树的后序遍历
Geekhyt opened this issue · 0 comments
Geekhyt commented
后序遍历:先打印当前节点的左子树,再打印当前节点的右子树,最后打印当前节点 (CDBFGEA)。
const postorderTraversal = function(root) {
const result = [];
function pushRoot(node) {
if (node !== null) {
if (node.left !== null) {
pushRoot(node.left);
}
if (node.right !== null) {
pushRoot(node.right);
}
result.push(node.val);
}
}
pushRoot(root);
return result;
};- 时间复杂度: O(n)
- 空间复杂度: O(n)
