二叉树的循环遍历方法,Java实现。利用栈和arraylist

记忆法:if 的位置
先序遍历: if   while
中序遍历:while  if  else

后序遍历:do while while if else break while

package sword.to.offer;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

class BinaryTree{
	int value;
	BinaryTree mLeft;
	BinaryTree mRight;
}
public class BinaryTreePreOrderPrint {
	

	//二叉树的非递归遍历算法(循环遍历)(前序遍历)
	public static  List<Integer> iterativelyPreOrderPrint(BinaryTree binaryTree){
		List<Integer> list=new ArrayList<Integer>();
		Stack<BinaryTree> stack=new Stack<BinaryTree>();
		if(binaryTree!=null) stack.push(binaryTree);
		while (!stack.isEmpty()) {
			binaryTree=stack.peek();
			stack.pop();
			list.add(binaryTree.value);

			if(binaryTree.mRight!=null) stack.push(binaryTree.mRight);
			if(binaryTree.mLeft!=null) stack.push(binaryTree.mLeft);
		}
		return list;
	}

	//二叉树的非递归遍历算法(循环遍历)(中序遍历)
	public static  List<Integer> iterativelyInOrderPrint(BinaryTree binaryTree){
		List<Integer> list=new ArrayList<Integer>();
		Stack<BinaryTree> stack=new Stack<BinaryTree>();
		while (!stack.isEmpty()||binaryTree!=null) {
			if(binaryTree!=null)
			{
				stack.push(binaryTree);
				binaryTree=binaryTree.mLeft;
			}
			else {
				binaryTree = stack.peek();
				stack.pop();
				list.add(binaryTree.value);
				binaryTree=binaryTree.mRight;
			}
		}
		return list;
	}
	//二叉树的非递归遍历算法(循环遍历)(后序遍历)
	public static  List<Integer> iterativelyPostOrderPrint(BinaryTree binaryTree){
		List<Integer> list=new ArrayList<Integer>();
		Stack<BinaryTree> stack=new Stack<BinaryTree>();

		
		
		do{
			while(binaryTree!=null){
				stack.push(binaryTree);
				binaryTree=binaryTree.mLeft;
			}
		BinaryTree bi = null;
			while(!stack.isEmpty())
			{
				if(binaryTree.mRight!=bi)
				{
					stack.push(binaryTree);
					binaryTree=binaryTree.mRight;
					break;
				}else {
					list.add(binaryTree.value);
					bi=binaryTree;
				}
			}
		}while(!stack.isEmpty());
		
		
		return list;
	}

//以下是求一个二叉树的镜像
<pre name="code" class="java" style="color: rgb(51, 51, 51); font-size: 14px; line-height: 26px;">public static void preOrderPrint(BinaryTree binaryTree) {
		if(binaryTree==null)
			return;
		System.out.println(binaryTree.value);
		preOrderPrint(binaryTree.mLeft);
		preOrderPrint(binaryTree.mRight);

	}
	public static void mirrorRecursivelyPreOrderPrint(BinaryTree binaryTree) {
		if(binaryTree==null)
			return;
		if(binaryTree.mLeft==null&&binaryTree.mRight==null)
		{
			if(binaryTree==null)
				return;
			else {
				System.out.println(binaryTree.value);
				return;
			}
		}
		System.out.println(binaryTree.value);
		swapNode(binaryTree);
		mirrorRecursivelyPreOrderPrint(binaryTree.mLeft);
		mirrorRecursivelyPreOrderPrint(binaryTree.mRight);

	}
	public static BinaryTree swapNode(BinaryTree binaryTree) {
		BinaryTree tree=new BinaryTree();
		tree=binaryTree.mLeft;
		binaryTree.mLeft=binaryTree.mRight;
		binaryTree.mRight=tree;

		return binaryTree;

	}
public static void main(String[] args) {BinaryTree binaryTree=new BinaryTree();binaryTree.value=10;BinaryTree binaryTree1=new BinaryTree();binaryTree1.value=9;binaryTree1.mLeft=null;BinaryTree binaryTree2=new BinaryTree();binaryTree2.value=11;binaryTree.mLeft=binaryTree1;binaryTree.mRight=binaryTree2;// mirrorRecursivelyPreOrderPrint(binaryTree);//测试空树BinaryTree treeNull=null;if(treeNull==null)System.out.println(treeNull+"treeNull为null");mirrorRecursivelyPreOrderPrint(treeNull);}}

 


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值