1 Star 0 Fork 0

ZTH-CS/Java

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
LevelOrderTraversalQueue.java 1.18 KB
一键复制 编辑 原始数据 按行查看 历史
package DataStructures.Trees;
import java.util.Queue;
import java.util.LinkedList;
/* Class to print Level Order Traversal */
public class LevelOrderTraversalQueue {
/* Class to represent Tree node */
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = null;
right = null;
}
}
/* Given a binary tree. Print its nodes in level order
using array for implementing queue */
void printLevelOrder(Node root) {
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty()) {
/* poll() removes the present head.
For more information on poll() visit
http://www.tutorialspoint.com/java/util/linkedlist_poll.htm */
Node tempNode = queue.poll();
System.out.print(tempNode.data + " ");
/*Enqueue left child */
if (tempNode.left != null) {
queue.add(tempNode.left);
}
/*Enqueue right child */
if (tempNode.right != null) {
queue.add(tempNode.right);
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zth-cs/Java.git
git@gitee.com:zth-cs/Java.git
zth-cs
Java
Java
master

搜索帮助