首页 > 代码库 > 二叉树三种遍历(递归以及非递归实现)

二叉树三种遍历(递归以及非递归实现)

package com.shiyeqiang.tree;

import java.util.Stack;

public class BiTree {

	public static void main(String[] args) {

		// 首先构造叶子节点
		BiTree leafA1 = new BiTree(4);
		BiTree leafA2 = new BiTree(5);
		BiTree leafB1 = new BiTree(6);
		BiTree leafB2 = new BiTree(7);
		// 构建二叉树的结构
		BiTree treeA = new BiTree(2, leafA1, leafA2);
		BiTree treeB = new BiTree(3, leafB1, leafB2);
		BiTree tree = new BiTree(1, treeA, treeB);

		System.out.println("递归前序遍历二叉树结果: ");
		preOrder(tree);
		System.out.println();
		System.out.println("非递归前序遍历二叉树结果: ");
		iterativePreOrder(tree);
		System.out.println();
		System.out.println("非递归中序遍历二叉树结果: ");
		iterativeInOrder(tree);
		System.out.println();
		System.out.println("递归后续遍历二叉树结果: ");
		iterativePostOrder(tree);
	}

	private BiTree leftTree;
	private BiTree rightTree;
	private Object data;

	public BiTree() {

	}

	public BiTree(Object data) {
		this.data = http://www.mamicode.com/data;>