后序遍历
递归实现
const postOrder = function (root, array = []) {
if (root) {
postOrder(root.left, array);
postOrder(root.right, array);
array.push(root.val);
}
return array;
};非递归实现
const postOrder = function (root) {
const result = [];
const stack = [];
let last = null; // 标记上一个访问的节点
let current = root;
while (current || stack.length > 0) {
while (current) {
stack.push(current);
current = current.left;
}
current = stack[stack.length - 1];
if (!current.right || current.right == last) {
current = stack.pop();
result.push(current.val);
last = current;
current = null; // 继续弹栈
} else {
current = current.right;
}
}
return result;
}更好的实现,双栈
最后更新于