二叉树的先序,中序,后序递归和非递归实现

二叉树l结构类定义:


public class TreeNode {


	 int val ;
	TreeNode left ;
	TreeNode right ;
	
	public TreeNode(int val){
		this.val = val ;
		left = null ;
		right = null ;
	}
}




测试二叉树:
                        1
              2                    3 
       4          5       6             7
  8        10
     9
先序遍历
public class PreOrder {
	
	//递归方法
	public  static void preorder1(TreeNode root){
		if(root == null) return ;
		System.out.print(root.val + " ");
		if(root.left!=null) preorder1(root.left);		
		if(root.right != null) preorder1(root.right);
	}
	
	//非递归方法
	public static void preorder2(TreeNode root){
		LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
		if(root ==  null) return ;
		TreeNode p = root ;
		stack.push(p);
		while(!stack.isEmpty()){
			TreeNode temp = stack.pop();
			System.out.print(temp.val + " ");
			if(temp.right != null) stack.push(temp.right);
			if(temp.left != null) stack.push(temp.left);
		}
		
	}
	
	
	public static void main(String[] args){
		TreeNode root = GeneratorTree.getTree();
		System.out.print("递归先序遍历:");
		preorder1(root);
		
		System.out.println();
		
		System.out.print("非递归先序遍历:");
		preorder2(root);
	}


}


输出:
递归先序遍历:1 2 4 8 9 10 5 3 6 7 
非递归先序遍历:1 2 4 8 9 10 5 3 6 7 




中序遍历:
public class InOrder {


	//递归遍历
	public  static void inorder1(TreeNode root){
		if(root == null) return ;
		
		if(root.left!=null) inorder1(root.left);	
		System.out.print(root.val + " ");
		if(root.right != null) inorder1(root.right);
	}
	//非递归遍历
	public static void inorder2(TreeNode root){
		LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
		if(root == null ) return ;
		TreeNode p = root ; ;
		while(p != null || !stack.isEmpty()){
			if(p != null){
				stack.push(p);
				p = p.left;
			}
			else{
				p = stack.pop();
				System.out.print(p.val + " ");
				p = p.right ;
			}
		}
	}
	public static void main(String[] args){
		TreeNode root = GeneratorTree.getTree();
		System.out.print("递归中序遍历:");
		inorder1(root);
		
		System.out.println();
		
		System.out.print("非递归中序遍历:");
		inorder2(root);
	}


}


输出:
递归中序遍历:8 9 4 10 2 5 1 6 3 7 
非递归中序遍历:8 9 4 10 2 5 1 6 3 7 




后序遍历:
public class PostOrder {


	//递归遍历
	public  static void postorder1(TreeNode root){
		if(root == null) return ;
		
		if(root.left!=null) postorder1(root.left);	
		if(root.right != null) postorder1(root.right);
		System.out.print(root.val + " ");
	}
	//非递归遍历
	public static void postorder2(TreeNode root){
		if(root == null) return ;
		TreeNode p = root ;
		TreeNode pre = p ;                        //记录最近出栈元素
		LinkedList<TreeNode> stack = new LinkedList<TreeNode>();
		boolean flag = true ;                      //标记变量,表示当前是继续入栈还是出栈
		while( p!= null || !stack.isEmpty()){
			if(p!=null && flag == true){
				stack.push(p);
				p = p.left ;
			}
			else{
				if(stack.isEmpty()) return ;
				p = stack.peek() ;
				if(p.right != null && p.right != pre){
					p = p.right ;
					flag = true ;
				}
				else{
					p = stack.pop();
					System.out.print(p.val + " ");
					pre = p ;
					flag = false ;
				}
			}
			
		}
	}
	public static void main(String[] args){
		TreeNode root = GeneratorTree.getTree();
		System.out.print("递归后序遍历:");
		postorder1(root);
		
		System.out.println();
		
		System.out.print("非递归后序遍历:");
		postorder2(root);
	}
}


输出:
递归后序遍历:9 8 10 4 5 2 6 7 3 1 
非递归后序遍历:9 8 10 4 5 2 6 7 3 1 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值