树的遍历
来源:互联网 发布:布谷鸟算法 编辑:程序博客网 时间:2024/05/20 15:37
1、层序遍历
public class Solution { ArrayList<Integer> list = new ArrayList<>(); ArrayList<TreeNode> nodes = new ArrayList<>(); public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { if(root !=null){ nodes.add(root); while(nodes.size() !=0){ TreeNode n = nodes.remove(0);//删除并返回头结点 list.add(n.val); if(n.left!=null){ nodes.add(n.left); } if(n.right!=null){ nodes.add(n.right); } } } return list; }}
2、前序遍历
public class Solution { ArrayList<Integer> list = new ArrayList<>(); public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) { if(root !=null){ list.add(root.val); //根 PrintFromTopToBottom(root.left); //左 PrintFromTopToBottom(root.right); //右 } retrun list;}
阅读全文
0 0
- 二叉树的遍历(层遍历和深度遍历)
- 二叉树的前序中序后序遍历,非递归遍历 层次遍历
- 二叉树的先中后序遍历,递归遍历,非递归遍历
- 二叉树遍历-----前序后序迭代遍历的新思路
- 二叉树的遍历-按层次遍历
- 二叉树的遍历(递归遍历)
- 二叉树的遍历-层次遍历
- 树的遍历
- 六、 树的遍历
- 二叉树的遍历
- haffman树的遍历
- 二叉树的遍历
- 树的各种遍历
- 树的遍历
- 二叉树的遍历
- C++树的遍历
- Java树的遍历
- 树的简单遍历
- spring boot实战(第三篇)事件监听源码分析
- 华为打开日志方法
- LinkedList源码分析
- Hadoop之Hive
- 每天回顾linux命令(date)
- 树的遍历
- 操作系统安装
- listview的单选
- 不用加减乘除做加法
- PrintWriter 返回前端中文乱码
- 【云隐】windows下编译及使用libevent
- CSS3瀑布流
- Docker获取镜像报错 docker: Error response from daemon: unauthorized: incorrect username or password.
- HashMap源码分析