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

来源:互联网 发布:魔兽争霸mac无法打开 编辑:程序博客网 时间:2024/06/06 01:45
记忆法: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);}}



1 0
原创粉丝点击