原题链接
typescript
function postorderTraversal(root: TreeNode | null): number[] {
let current = root;
const answer = [];
const stack = [];
let prev = null;
while(current || stack.length) {
while(current) {
stack.push(current);
current = current.left;
}
if(stack.length) {
current = stack.pop();
}
if(!current.right || current.right === prev) {
// 没有右子树 或者 右子树已经访问完毕
answer.push(current.val);
prev = current;
current = null;
} else {
stack.push(current); // 有右子树 再重新保存下 待会返回后来用
current = current.right;
}
}
return answer;
};