博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA实现二叉树的层次遍历问题
阅读量:4041 次
发布时间:2019-05-24

本文共 3957 字,大约阅读时间需要 13 分钟。

JAVA实现二叉树的层次遍历问题

所谓二叉树的层次遍历顺序:首先访问根节点,接着访问层数1上的节点,随后访问层数2上的节点,依次类推。

如图所示:

这里写图片描述

层次遍历:A、B、C、D、E、null,F,G,null

算法设计

1、首先定义树节点

public class TreeNode {    int data; //节点值    TreeNode leftChild; //左子树节点    TreeNode rightChild; //右子树节点    //构造方法    TreeNode(){        this.leftChild=null;        this.rightChild=null;        this.data=-1;    }    //构造方法    TreeNode(int data){        this.leftChild=null;        this.rightChild=null;        this.data=data;    }    //构造方法    public TreeNode(int data, TreeNode leftChild, TreeNode rightChild) {        this.data = data;        this.leftChild = leftChild;        this.rightChild = rightChild;    }    //以下为get***和set***方法    public int getData() {        return data;    }    public void setData(int data) {        this.data = data;    }    public TreeNode getLeftChild() {        return leftChild;    }    public void setLeft(TreeNode leftChild) {        this.leftChild = leftChild;    }    public TreeNode getRightChild() {        return rightChild;    }    public void setRight(TreeNode rightChild) {        this.rightChild = rightChild;    }}

2、层次遍历算法

/*     * 二叉树的层次遍历     * */    public static ArrayList
> levelOrder(TreeNode root){ ArrayList
> result = new ArrayList
>(); if(root == null){ return result; } Queue
queue = new LinkedList
(); queue.offer(root); while(!queue.isEmpty()){ int size = queue.size(); ArrayList
level = new ArrayList
(); for(int i = 0;i < size ;i++){ TreeNode node = queue.poll(); level.add(node.data); if(node.leftChild != null){ queue.offer(node.leftChild); } if(node.rightChild != null){ queue.offer(node.rightChild); } } result.add(level); } System.out.println(result); return result; }

3、测试代码

以下面的数组创建一颗二叉树,二叉树的形态如图所示:

这里写图片描述

层次遍历代码

public class CreateBinaryTree {    private int[] array = {
3,9,20,0,0,15,7}; private static List
nodeList = null; public void createBinTree() { nodeList = new LinkedList
(); // 将一个数组的值依次转换为Node节点 for (int nodeIndex = 0; nodeIndex < array.length; nodeIndex++) { nodeList.add(new TreeNode(array[nodeIndex])); } // 对前lastParentIndex-1个父节点按照父节点与孩子节点的数字关系建立二叉树 for (int parentIndex = 0; parentIndex < array.length / 2 - 1; parentIndex++) { // 左孩子 nodeList.get(parentIndex).leftChild = nodeList .get(parentIndex * 2 + 1); // 右孩子 nodeList.get(parentIndex).rightChild = nodeList .get(parentIndex * 2 + 2); } // 最后一个父节点:因为最后一个父节点可能没有右孩子,所以单独拿出来处理 int lastParentIndex = array.length / 2 - 1; // 左孩子 nodeList.get(lastParentIndex).leftChild = nodeList .get(lastParentIndex * 2 + 1); // 右孩子,如果数组的长度为奇数才建立右孩子 if (array.length % 2 == 1) { nodeList.get(lastParentIndex).rightChild = nodeList .get(lastParentIndex * 2 + 2); } } /* * 二叉树的层次遍历 * */ public static ArrayList
> levelOrder(TreeNode root){ ArrayList
> result = new ArrayList
>(); if(root == null){ return result; } Queue
queue = new LinkedList
(); queue.offer(root); while(!queue.isEmpty()){ int size = queue.size(); ArrayList
level = new ArrayList
(); for(int i = 0;i < size ;i++){ TreeNode node = queue.poll(); level.add(node.data); if(node.leftChild != null){ queue.offer(node.leftChild); } if(node.rightChild != null){ queue.offer(node.rightChild); } } result.add(level); } System.out.println(result); return result; } public static void main(String[] args) { // TODO Auto-generated method stub CreateBinaryTree createBinaryTree=new CreateBinaryTree(); createBinaryTree.createBinTree(); TreeNode root = nodeList.get(0); System.out.println("二叉树的层次遍历为:"); levelOrder(root); System.out.println("------------------------------------"); }}

(完)

你可能感兴趣的文章
Java.nio
查看>>
函数模版类模版和偏特化泛化的总结
查看>>
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>