左神算法笔记之二叉树——二叉树中序先序后序遍历

一、二叉树递归和非递归方式实现先序、中序和后序遍历

public class PreInPosTraversal {
	public static class Node{
		public int value;
		public Node left;
		public Node right;

		public Node(int data){
			this.value = data;
	}
	
	//递归:
	//先序:头左右
	public static void preOrderRecur(Node head){
		if(head == null){
			return;
		}
		System.out.print(head.value+" ");
		preOrderRecur(head.left);
		preOrderRecur(head.right);
	}
	//中序:左头右
	public static void inOrderRecur(Node head){
		if(head == null){
			return;
		}
		inOrderRecur(head.left);
		System.out.print(head.value+" ");
		inOrderRecur(head.right);
	}
	//后序
	public static void posOrderRecur(Node head){
		if(head == null){
			return;
		}
		posOrderRecur(head.left);
		posOrderRecur(head.right);
		System.out.print(head.value+" ");
	}

	//非递归:
	//先序:1.准备一个栈,放进头节点,从栈中弹出一个节点cur
	//     2.打印cur
	//     3.先右再左节点压入栈(如果有)
	//     4.重复上述过程至栈为空
	public static void preOrderUnRecur(Node head){
		System.out.print("pre-order:");
		if(head != null){
			Stack<Node> stack = new Stack<Node>();
			stack.add(head);
			while(!stack.isEmpty()){
				head = stack.pop();
				System.out.print(head.value+" ");
				if(head.right != null){
					stack.push(head.right);
				}
				if(head.left != null){
					stack.push(head.left);
				}
			}
		}
		System.out.println();
	}

	//后序:
    //准备两个栈
    //先头节点压入第一个栈,弹出到收集栈
    //然后先左后右节点压入栈
    //重复上述过程,最后依次弹出收集栈的元素
	public static void posOrderUnRecur(Node head){
        System.out.print("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(head);//进入收集栈
                //先左节点进栈
                if(head.left != null){
                    s1.push(head.left);
                }
                //后右节点进栈
                if(head.right != null){
                    s1.push(head.right);
                }
            }
            //按顺序弹出收集栈栈顶节点
            while(!s2.isEmpty()){
                System.out.print(s2.pop().value+" ");
            }
        }
        System.out.println();
    }
	

	//中序:按左中右
    //先是每棵子树的的整个左边界进栈
    //然后依次弹出,弹出并打印(已包括根节点)
    //每弹出一个节点时要检查是否有右子树,如果有
    //右子树重复上述过程,重复完继续弹出
	public static void inOrderunRecur(Node head){
        System.out.print("in-order");
        if(head != null){
            Stack<Node> stack = new Stack<Node>();
            while(!stack.isEmpty() || head !=null){
                //当head不为空时,就将它的左子树放进栈中
                if(head != null){
                    stack.push(head);
                    head = head.left;
                //当head为空,stack不为空时,弹出一个栈顶元素,然后打印,并看看他有没有右子树
                }else{
                    head = stack.pop();
                    System.out.print(head.value+" ");
                    head = head.right;
                }
            }
        }
        System.out.println();
    }
}
		   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值