Java数据结构系列之——树(六):二叉树的层序遍历
Java数据结构系列之——树(6):二叉树的层序遍历
package tree.binarytree; import java.util.LinkedList; /** * 层序遍历二叉树 * * @author wl * */ public class PrintFromTopToBotton { public static void printfromtoptobotton(BiTreeNode root) { if (root == null) { return; } LinkedList<BiTreeNode> queue = new LinkedList<BiTreeNode>(); queue.addLast(root); while (queue.size() != 0) { BiTreeNode current = queue.pollFirst(); System.out.println(current.data); if (current.leftNode != null) { queue.addLast(current.leftNode); } if (current.rightNode != null) { queue.addLast(current.rightNode); } } } }