Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>
问题描述:
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3
/ \
9 20
/ \
15 7
return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
算法分析:三种方法。这道题和普通的层序遍历不一样的地方就是,需要每层单独输出。因此可以考虑用来两个队列,每个队列存放不同层的节点。我自己写的代码有重复的结构,并不友好。参考网上,其实将第二个队列赋值给第一个队列就行,就不会有重复代码了。也无需考虑队列空的情况了。第三种方法其实就是按层遍历的一种思想,是第二种方法的优化。
public class BinaryTreeLevelOrderTraversal
{
public List<List<Integer>> levelOrder(TreeNode root)
{
List<List<Integer>> res = new ArrayList<>();
if(root == null)
{
return res;
}
Deque<TreeNode> queue1 = new ArrayDeque<>();
Deque<TreeNode> queue2 = new ArrayDeque<>();
queue1.offer(root);
while(!queue1.isEmpty() || !queue2.isEmpty())
{
List<Integer> list1 = new ArrayList<>();
while(!queue1.isEmpty())
{
TreeNode temp = queue1.poll();
if(temp.left != null)
{
queue2.offer(temp.left);
}
if(temp.right != null)
{
queue2.offer(temp.right);
}
list1.add(temp.val);
}
if(list1.size() != 0)//队列空的时候,list1的大小为0
res.add(list1);
List<Integer> list2 = new ArrayList<>();
while(!queue2.isEmpty())
{
TreeNode temp = queue2.poll();
if(temp.left != null)
{
queue1.offer(temp.left);
}
if(temp.right != null)
{
queue1.offer(temp.right);
}
list2.add(temp.val);
}
if(list2.size() != 0)
res.add(list2);
} return res;
} public List<List<Integer>> levelOrder2(TreeNode root)
{
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
if(root == null)
{
return res;
} ArrayDeque<TreeNode> curr = new ArrayDeque<>();
ArrayDeque<TreeNode> next = new ArrayDeque<>(); curr.offer(root); while(!curr.isEmpty())
{
TreeNode temp = curr.poll();
if(temp.left != null)
{
next.offer(temp.left);
}
if(temp.right != null)
{
next.offer(temp.right);
} list.add(temp.val); if(curr.isEmpty())
{
res.add(list);
list = new ArrayList<Integer>();
curr = next;
next = new ArrayDeque<>();
}
} return res;
} public List<List<Integer>> levelOrder3(TreeNode root)
{
List<List<Integer>> res = new ArrayList<>(); if(root == null)
{
return res;
}
ArrayList<TreeNode> arr = new ArrayList<>();
arr.add(root);
while(!arr.isEmpty())
{
List<Integer> list = new ArrayList<>();
ArrayList<TreeNode> temp = new ArrayList<>();
for (TreeNode node : arr)
{
list.add(node.val);
if(node.left != null)
{
temp.add(node.left);
}
if(node.right != null)
{
temp.add(node.right);
}
}
res.add(list);
arr = temp;
} return res;
} public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>(); Deque<TreeNode> curr = new ArrayDeque<>();
Deque<TreeNode> next = new ArrayDeque<>(); if(root == null)
{
return res;
} curr.offer(root);
while(! curr.isEmpty())
{
TreeNode temp = curr.poll(); if(temp.left != null)
{
next.offer(temp.left);
}
if(temp.right != null)
{
next.offer(temp.right);
} list.add(temp.val); if(curr.isEmpty())
{
res.add(list);
list = new ArrayList<>();
curr = next;
next = new ArrayDeque<>();
}
} List<List<Integer>> reverseRes = new ArrayList<>();
for(int i = res.size()-1; i >= 0; i --)
{
reverseRes.add(res.get(i));
}
return reverseRes;
}
}
Binary Tree Level Order Traversal,层序遍历二叉树,每层作为list,最后返回List<list>的更多相关文章
- LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...
- 102. Binary Tree Level Order Traversal ------层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to righ ...
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- 102. Binary Tree Level Order Traversal 广度优先遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode OJ:Binary Tree Level Order Traversal II(二叉树的层序遍历)
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode 107. Binary Tree Level Order Traversal II (二叉树阶层顺序遍历之二)
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8
102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...
- [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- [LeetCode] 102. Binary Tree Level Order Traversal 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
随机推荐
- 【BZOJ2809】[Apio2012]dispatching 可并堆
[BZOJ2809][Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 M ...
- 利用aspose-words 实现 java中word转pdf文件
利用aspose-words 实现 java中word转pdf文件 首先下载aspose-words-15.8.0-jdk16.jar包 引入jar包,编写Java代码 package test; ...
- mysql 修改配置文件性能优化
vim /etc/my.cnf 原配置文件 [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # ...
- MySQL数据的查询注意
mysql数据库的查询结果输出的是一个记录,这一个记录是以字典的形式存储的,所以获取字段时要用字典提取数据的形式获取字段信息: 查询的代码: def query(self): # sql语句 sql ...
- Android复习
第一章 1.Android系统架构:(四层) Linux内核层:为安卓设备的各种硬件提供底层的驱动. 系统运行库层:为Android系统提供主要的特性支持. 应用框架层:提供了构建应用程序可能用到的各 ...
- Oracle监控的关键指标
1.监控事例的等待 select event, , , )) "Prev", , , )) "Curr", count(*) "Tot" f ...
- linux 如何查看防火墙是否开启
service iptables status可以查看到iptables服务的当前状态.但是即使服务运行了,防火墙也不一定起作用,你还得看防火墙规则的设置 iptables -L在此说一下关于启动和关 ...
- 9.python的列表
list2 = [1, 2, 3, 4, 5, 6, 7 ]; print ("list2[1:5]: ", list2[1:5]) 得到 list2[1:5]: [2, 3, ...
- Openstack(十四)创建虚拟机
启动虚拟机之前需要先做一些前期准备,比如网络和IP地址分配.虚拟机:类型创建等等,具体如下: 14.1网络规划及IP划分 #官网安装文档:https://docs.openstack.org/ocat ...
- WHCTF2017线上小记
第四届XCTF开始,首战因素,加上团队刚加入了两个新人的原因,还是决定一块参与一下.水了3题.2个RE和1个MISC,照顾新人,写的比较啰嗦. [MISC] PY-PY-PY 下载题目之后是一个pyc ...