//二叉树后序遍历
public class Node{
public int value;
public Node left;
public Node right;
public Node(int data){
this.value=data;
}
}
//递归实现二叉树后序遍历
public void posOrderRecur(Node head){
if(head==null){
return;
}
System.out.println(head.value + " ");
posOrderRecur(head.left);
posOrderRecur(head.right);
}
//非递归实现二叉树后序遍历
public void posOrderUnRecur(Node head){
System.out.println("pos-order: ");
if(head!=null){
Stack<Node> s1=new Stack<Node>();
Stack<Node> s2=new Stack<Node>();
s1.push(head);
while(!s1.isEmpty()){
head=s1.pop();
s2.push();
if(head.left!=null){
s1.push(head.left);
}
if(head.right!=null){
s1.push(head.right);
}
}
while(!s2.isEmpty()){
System.out.println(s2.pop().value + " ");
}
}
System.out.println();
}
二叉树-后序遍历
猜你喜欢
转载自blog.csdn.net/weixin_42146769/article/details/88422873
今日推荐
周排行