
public static void preOrderUnRecur(Node head) {
System.out.println("先序遍历如下:");
if(head!=null) {
Stack s=new Stack();
s.push(head); //压入根节点
while(!s.isEmpty()) {
head=s.pop(); //弹出节点
System.out.print(head.data+" ");
if(head.right!=null) { //压入右孩子
s.push(head.right);
}
if(head.left!=null) { //压入左孩子
s.push(head.left);
}
}
}
}


public static void inOrderUnRecur(Node head) {
System.out.println("中序遍历如下:"); //左根右 -----一直压入左 左出 根 右孩子
if(head!=null) {
Stack s=new Stack();
while(!s.isEmpty() || head!=null) {
if(head!=null) { //头节点不为空 则压入
s.push(head); //不断压入左孩子 直到为空(没有左孩子)
head=head.left;
}else { //开始弹出之前的左孩子 (左---根)
head=s.pop();
System.out.print(head.data+" ");
head=head.right; //右孩子 之后压入 弹出
}
}
}
}

/*两个栈实现后序*/
public static void postOrderUnRecur(Node head) {
System.out.println("后序遍历如下:");
if(head!=null) {
Stack s1=new Stack (); //s1 入 根-- 左右 -(类似于先序遍历)--- 出 根--右左
Stack s2=new Stack(); //s2 出 左右根 为后序
s1.push(head); //压入根节点
while(!s1.isEmpty()) {
head=s1.pop(); //s1弹出节点
s2.push(head); //s2压入s1刚弹出的节点
if(head.left!=null) {
s1.push(head.left);
}
if(head.right!=null) {
s1.push(head.right);
}
}
//s1弹出完毕 s2压入完毕 S2该弹出了 并打印
while(!s2.isEmpty()) {
System.out.print(s2.pop().data+" ");
}
}



