码迷,mamicode.com
首页 > 其他好文 > 详细

二叉树的遍历,递归和非递归

时间:2016-07-29 21:27:56      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

import java.util.ArrayDeque;
import java.util.Stack;

class TreeNode {
	public int val;
	public TreeNode left;
	public TreeNode right;
	public TreeNode(int val)
	{
		this.val = val;
	}
}
public class BinaryTree
{
	//先序遍历递归
	public static void preOrder(TreeNode t)
	{
		if(t == null)
		{
			return;
		}
		System.out.print(t.val+" ");
		preOrder(t.left);
		preOrder(t.right);
	}
	//中序遍历递归
	public static void inOrder(TreeNode t)
	{
		if(t == null)
		{
			return;
		}
		inOrder(t.left);
		System.out.print(t.val+" ");
		inOrder(t.right);
	}	
	//后序遍历递归
	public static void postOrder(TreeNode t)
	{
		if(t == null)
		{
			return;
		}
		postOrder(t.left);
		postOrder(t.right);
		System.out.print(t.val+" ");
	}	
	
	
	//先序遍历非递归
	public static void preOrder2(TreeNode t)
	{
		Stack<TreeNode> s = new Stack<>();
		while(t!=null || !s.isEmpty())
		{
			while(t != null)//遍历根节点和左子树
			{
				System.out.print(t.val+" ");
				s.push(t);
				t = t.left;
			}
			if(!s.isEmpty())//遍历右孩子
			{
				t = s.pop();
				t = t.right;
			}
		}
	}
	
	//中序遍历非递归
	public static void inOrder2(TreeNode t)
	{
		Stack<TreeNode> s = new Stack<>();
		while(t!=null || !s.isEmpty())
		{
			while(t != null)
			{
				s.push(t);
				t = t.left;
			}
			if(!s.isEmpty())
			{
				t = s.pop();
				System.out.println(t.val);
				t = t.right;
			}
		}
	}
	
	//后序遍历非递归
	public static void postOrder2(TreeNode t)
	{
		Stack<TreeNode> s = new Stack<>();
		Stack<TreeNode> result = new Stack<>();//存储逆后序遍历结果
		while(t != null || !s.isEmpty())
		{
			while(t != null)
			{
				s.push(t);
				result.push(t);
				t = t.right;
			}
			if(!s.isEmpty())
			{
				t = s.pop();
				t = t.left;
			}
		}
		while(!result.isEmpty())
		{
			System.out.print(result.pop().val+" ");
		}
	}
	
	
	//层序遍历
	public static void levelTraverse(TreeNode t)
	{
		ArrayDeque<TreeNode> queue = new ArrayDeque<>();
		if(t == null)
		{
			return;
		}
		queue.offer(t);
		while(!queue.isEmpty())
		{
			t = queue.poll();
			System.out.print(t.val + " ");
			if(t.left != null)
			{
				queue.offer(t.left);
			}
			if(t.right != null)
			{
				queue.offer(t.right);
			}
		}
	}
	
	
	//螺旋遍历
	public static void spiralTraverse(TreeNode t)
	{
		Stack<TreeNode> s1 = new Stack<>();
		Stack<TreeNode> s2 = new Stack<>();
		if(t == null)
		{
			return;
		}
		s1.push(t);
		while(!s1.isEmpty() || !s2.isEmpty())
		{
			while(!s1.isEmpty())
			{
				t = s1.pop();
				System.out.print(t.val+" ");
				if(t.right!=null)
				{
					s2.push(t.right);
				}
				if(t.left!=null)
				{
					s2.push(t.left);
				}
			}
			while(!s2.isEmpty())
			{
				t = s2.pop();
				System.out.print(t.val+" ");
				if(t.left!=null)
				{
					s1.push(t.left);
				}
				if(t.right!=null)
				{
					s1.push(t.right);
				}
			}
		}
	}
	
	
}

 

二叉树的遍历,递归和非递归

标签:

原文地址:http://www.cnblogs.com/masterlibin/p/5719586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!