二叉树的遍历:先序中序后序遍历的递归与非递归实现

二叉树的节点声明如下:

class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode(int x){
		val = x;
		left = null;
		right = null;
	}
}

 1. 先序遍历:(前为递归实现,后为非递归实现)

public class shu {
	//树递归的先序遍历 (根 左 右)
	public static void main(String[] args) {
		TreeNode tn = new TreeNode(4);
		TreeNode tn1 = new TreeNode(5);
		TreeNode tn2 = new TreeNode(6);
		TreeNode tn3 = new TreeNode(7);
		TreeNode tn4 = new TreeNode(8);
		TreeNode tn5 = new TreeNode(9);
		TreeNode tn6 = new TreeNode(10);
		tn.left = tn1;
		tn.right = tn2;
		tn1.left=tn3;
		tn1.right=tn4;
		tn2.left=tn5;
		tn2.right=tn6;
		shu s = new shu();
		s.DPreOrderTraversal(tn);
	}

	public void DPreOrderTraversal(TreeNode root)
	{
		 if(root!=null){
	        System.out.println(root.val);     //对节点做些访问比如打印         
	        DPreOrderTraversal(root.left);     //访问左儿子
	        DPreOrderTraversal(root.right);    //访问右儿子
		 }
	}
}

 

public class shu {
	//树非递归的先序遍历      和      中序比较相似。
	//树非递归的先序遍历 (根 左 右)
	public static void main(String[] args) {
		TreeNode tn = new TreeNode(4);
		TreeNode tn1 = new TreeNode(5);
		TreeNode tn2 = new TreeNode(6);
		TreeNode tn3 = new TreeNode(7);
		TreeNode tn4 = new TreeNode(8);
		TreeNode tn5 = new TreeNode(9);
		TreeNode tn6 = new TreeNode(10);
		tn.left = tn1;
		tn.right = tn2;
		tn1.left=tn3;
		tn1.right=tn4;
		tn2.left=tn5;
		tn2.right=tn6;
		shu s = new shu();
		s.PreOrderTraversal(tn);
	}
	public void PreOrderTraversal(TreeNode root)
	{
	    Stack<TreeNode> stack = new Stack<>();    //创建并初始化堆栈stack
	    while(!stack.empty() || root!=null)
	    {
	        while(root!=null)        
	        {
	        	System.out.println(root.val);//一直向左并将沿途节点访问(打印)后压入堆栈 
	        	stack.push(root);
	        	root = root.left;
	        }
	        root = stack.pop();
	        root = root.right;
	    } 
	}
}

2. 中序遍历:(前为递归实现,后为非递归实现)

public class shu {
	//树递归的中序遍历 (左 根  右)
	public static void main(String[] args) {
		TreeNode tn = new TreeNode(4);
		TreeNode tn1 = new TreeNode(5);
		TreeNode tn2 = new TreeNode(6);
		TreeNode tn3 = new TreeNode(7);
		TreeNode tn4 = new TreeNode(8);
		TreeNode tn5 = new TreeNode(9);
		TreeNode tn6 = new TreeNode(10);
		tn.left = tn1;
		tn.right = tn2;
		tn1.left=tn3;
		tn1.right=tn4;
		tn2.left=tn5;
		tn2.right=tn6;
		shu s = new shu();
		s.DMidOrderTraversal(tn);
	}

	public void DMidOrderTraversal(TreeNode root)
	{
		 if(root!=null){
			 DMidOrderTraversal(root.left);     //访问左儿子
	        System.out.println(root.val);     //对节点做些访问比如打印         
	        
	        DMidOrderTraversal(root.right);    //访问右儿子
		 }
	}
}

 

public class shu {
	//树非递归的先序遍历      和      中序比较相似。
	//树非递归的中序遍历 (左 根  右)
	public static void main(String[] args) {
		TreeNode tn = new TreeNode(4);
		TreeNode tn1 = new TreeNode(5);
		TreeNode tn2 = new TreeNode(6);
		TreeNode tn3 = new TreeNode(7);
		TreeNode tn4 = new TreeNode(8);
		TreeNode tn5 = new TreeNode(9);
		TreeNode tn6 = new TreeNode(10);
		tn.left = tn1;
		tn.right = tn2;
		tn1.left=tn3;
		tn1.right=tn4;
		tn2.left=tn5;
		tn2.right=tn6;
		shu s = new shu();
		s.MidOrderTraversal(tn);
	}
	public void MidOrderTraversal(TreeNode root)
	{
	    Stack<TreeNode> stack = new Stack<>();    //创建并初始化堆栈stack
	    while(!stack.empty() || root!=null)
	    {
	        while(root!=null)        
	        {
	        	stack.push(root);
	        	root = root.left;
	        }
	        root = stack.pop();
	        System.out.println(root.val);
	        root = root.right;
	    }  
	}
}

3. 后序遍历:(前为递归实现,后为非递归实现)

public class shu {
	//树递归的后序遍历 (左 右 根 )
	public static void main(String[] args) {
		TreeNode tn = new TreeNode(4);
		TreeNode tn1 = new TreeNode(5);
		TreeNode tn2 = new TreeNode(6);
		TreeNode tn3 = new TreeNode(7);
		TreeNode tn4 = new TreeNode(8);
		TreeNode tn5 = new TreeNode(9);
		TreeNode tn6 = new TreeNode(10);
		tn.left = tn1;
		tn.right = tn2;
		tn1.left=tn3;
		tn1.right=tn4;
		tn2.left=tn5;
		tn2.right=tn6;
		shu s = new shu();
		s.DPostOrderTraversal(tn);
	}

	public void DPostOrderTraversal(TreeNode root)
	{
		 if(root!=null){
			DPostOrderTraversal(root.left);    //访问左儿子
	        DPostOrderTraversal(root.right);     //访问右儿子
	        System.out.println(root.val);     //访问根    
	        
		 }
	}
}

 

public class shu {
	//树非递归的后序遍历 (左 右 根)
	public static void main(String[] args) {
		TreeNode tn = new TreeNode(4);
		TreeNode tn1 = new TreeNode(5);
		TreeNode tn2 = new TreeNode(6);
		TreeNode tn3 = new TreeNode(7);
		TreeNode tn4 = new TreeNode(8);
		TreeNode tn5 = new TreeNode(9);
		TreeNode tn6 = new TreeNode(10);
		tn.left = tn1;
		tn.right = tn2;
		tn1.left=tn3;
		tn1.right=tn4;
		tn2.left=tn5;
		tn2.right=tn6;
		List list = new ArrayList();
		shu s= new shu();
		list = s.postorderTraversal(tn);
		for(int i=0;i<list.size();i++){
			System.out.println(list.get(i));
		}
	}
	public List<Integer> postorderTraversal(TreeNode root){
		List res = new ArrayList<>();
		Stack<TreeNode> stack = new Stack<>();
		while(!stack.isEmpty()||root!=null){
			while(root!=null){
				res.add(0,root.val);
				stack.push(root);
				root = root.right;
			}
			root = stack.pop();
			root = root.left;
		}
		return res;
	}	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值