二叉树的循环遍历方法,Java实现。利用栈和arraylist
来源:互联网 发布:魔兽争霸mac无法打开 编辑:程序博客网 时间:2024/06/06 01:45
记忆法:if 的位置
先序遍历:if while
中序遍历:while if else
先序遍历: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
- 二叉树的循环遍历方法,Java实现。利用栈和arraylist
- 二叉树后序遍历的循环实现(JAVA)
- java 二叉树的实现 和遍历
- 二叉树的java实现和遍历
- java:ArrayList循环遍历的俩种方法使用
- 二叉树的高度 java 利用递归和层次遍历两种方法
- 二叉树的高度 java 利用递归和层次遍历两种方法
- C++二叉树遍历递归与栈循环的实现
- java:ArrayList循环遍历的俩种方法使用 ArrayList取出第i个值
- java:ArrayList循环遍历的俩种方法使用 ArrayList取出第i个值
- 二叉树的递归遍历和非递归(循环)遍历实现
- 利用栈实现二叉树的后序遍历算法
- 利用栈实现二叉树的后序遍历
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- java实现二叉树的建立及遍历方法
- 二叉树三种遍历方式的递归和循环实现
- Java实现二叉树的创建和遍历
- Linux学习之命令基本格式及文件处理命令
- hdu3220 Alice’s Cube--逆向BFS & 打表 & 位运算
- 【C语言】输出1000年到2000年之间的闰年
- SpringBoot学习笔记(4) Spring Boot 集成 Mybatis
- 说说进程与线程的区别与联系
- 二叉树的循环遍历方法,Java实现。利用栈和arraylist
- JavaWeb之Servlet
- json的定义
- 什么是数据库事务?
- Reverse Bits
- EventBus理解
- ServletContainerInitializer初始化器
- 基于Phaser框架完成FlappyBird
- 连接数据库