二叉树的层次遍历
来源:互联网 发布:贝佳斯绿泥淘宝 编辑:程序博客网 时间:2024/06/05 20:06
进行层次遍历时,对一层结点访问完后,再按照它们的访问次序对各个结点的左孩子右孩子顺序访问,这样就完成了对下一层从左往右访问。
具体步骤如下:
(1)初始化一个队列;
(2)将根节点放入队列;
(3)重复步骤4~7直到队列为空;
(4)从队列中取出一个结点x;
(5)访问x;
(6)如果x存在左孩子,将左孩子放入队列;
(7)如果x存在右孩子,将右孩子放入队列;
public void levelorder(BinaryTreeNode node) { if(node==null) return; Queue<BinaryTreeNode> queue=new LinkedList<BinaryTreeNode>(); queue.offer(node); while(!queue.isEmpty()) { BinaryTreeNode temp=queue.poll(); //弹出节点 System.out.println(temp.value); if(temp.left!=null) queue.offer(temp.left); if(temp.right!=null) queue.offer(temp.right); } }
阅读全文
0 0
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历!!!
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 二叉树的层次遍历
- 欢迎使用CSDN-markdown编辑器
- 3.3 if 条件分支语句
- Cg Programming/Unity/Translucent Surfaces半透明表面
- sizeof
- The Accomodation of Students
- 二叉树的层次遍历
- Ring3层的DLL注入(32)(64)
- Win10禁用管理员帐户后无法进入的两种解决方法
- Linux下的lds链接脚本解释
- NOIP2017 PJ蒟蒻的游记
- 从架构演进的角度聊聊 Spring Cloud 都做了些什么?
- 使用pycharm import本地文件夹的py文件时出现unresolved reference,下面一直出现红线
- Cad二次开发(二)
- Null pointer access: The variable al can only be null at this location问题的解决