HDU1622—二叉树的层次遍历的JAVA实现
时间: 2024-05-25 16:06:30 浏览: 9
以下是HDU1622—二叉树的层次遍历的JAVA实现:
```java
import java.util.LinkedList;
public class LevelOrder {
public void levelIterator(BiTree root) {
if (root == null) {
return;
}
LinkedList<BiTree> queue = new LinkedList<BiTree>();
BiTree current = null;
queue.offer(root); // 将根节点入队
while (!queue.isEmpty()) {
current = queue.poll(); // 出队队头元素并访问
System.out.print(current.val + "-->");
if (current.left != null) // 如果当前节点的左节点不为空入队
{
queue.offer(current.left);
}
if (current.right != null) // 如果当前节点的右节点不为空,把右节点入队
{
queue.offer(current.right);
}
}
}
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)