2021/05/03
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
LinkedList<TreeNode> stk = new LinkedList<>();
TreeNode p = root;
TreeNode r = null;
List<Integer> res = new ArrayList<>();
while (p != null || !stk.isEmpty()) {
while (p != null) {
stk.push(p);
p = p.left;
}
p = stk.peek();
if (p.right == null || p.right == r) {
res.add(p.val);
r = p;
stk.pop();
p = null;
} else {
p = p.right;
}
}
return res;
}
}