首页 > 代码库 > 二叉树三种遍历(递归以及非递归实现)
二叉树三种遍历(递归以及非递归实现)
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;>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。