//后续遍历的非递归实现
public void postOrder(TreeNode root){
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p = root;
stack.push(p);
TreeNode pre = null;
while (!stack.isEmpty()) {
TreeNode cur = stack.peek();
if ((cur.left==null && cur.right==null) || (pre!=null && (pre==cur.left || pre==cur.right))) {
System.out.println(cur.val);
pre = stack.pop();
}
else{
if (p.right!=null) {
stack.push(p.right);
}
if (p.left!=null) {
stack.push(p.left);
}
}
}
}
public void postOrder(TreeNode root){
Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode p = root;
stack.push(p);
TreeNode pre = null;
while (!stack.isEmpty()) {
TreeNode cur = stack.peek();
if ((cur.left==null && cur.right==null) || (pre!=null && (pre==cur.left || pre==cur.right))) {
System.out.println(cur.val);
pre = stack.pop();
}
else{
if (p.right!=null) {
stack.push(p.right);
}
if (p.left!=null) {
stack.push(p.left);
}
}
}
}