LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树
Binary Tree Level Order Traversal
题目描述:
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,#,#,15,7}
,3
/ \
9 20
/ \
15 7return its level order traversal as:
[
[3],
[9,20],
[15,7]
]
题目解答:
/**
* 实质是二叉树的广度优先搜索
* 利用一个辅助队列保存被访问的当前节点的左右孩子,
* 调整进出队列的顺序以实现层序遍历
*/
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result=new ArrayList<>(); if(root==null){
return result;
} /**
* 一般来说,队列的实现选择LinkedList即可
* 队列保存节点就不用找什么变量了
*/
Queue<TreeNode> queue=new LinkedList<>();
queue.offer(root);
//首先把头结点的值保存到结果集,然后把左右子节点分别进入队列
while(!queue.isEmpty()){//需要使用isEmptyy判断,不能使用null
List<Integer> temp=new ArrayList<>();
/**
* error!这里对进行循环的过程,队列长度是在不断变化的
* size需要等于队列出队前的长度
*/
int size =queue.size();
for(int i=0;i<size;i++){
TreeNode node=queue.poll();
temp.add(node.val);
if(node.left!=null)
queue.offer(node.left);
if(node.right!=null)
queue.offer(node.right);
}
//当前队列全部poll,到这里已经完成了一层的遍历
result.add(temp);
}
return result;
}
LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树的更多相关文章
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
- 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 ...
- 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 二叉树层序遍历 II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- Java for 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 二叉树层序遍历
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- LeetCode(32)-Binary Tree Level Order Traversal
题目: LeetCode Premium Subscription Problems Pick One Mock Articles Discuss Book fengsehng 102. Binary ...
- (二叉树 BFS) 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】Binary Tree Level Order Traversal I & II
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
随机推荐
- PYTHON 迭代器
可以走直接作用于for循环的对象统称为可迭代对象使用:Iterable 使用isinstance()判断一个对象是否是Iterable对象: from collections import Itera ...
- html5——canvas画直线
<html> <head> <title>canvas demo</title> </head> <body> <canv ...
- kill新号专题
一.在tomcat启动脚本中看到,kill -3
- Git SourceTree 冲突解决方案
Git现在越来越火,很多人都从Svn迁移到Git上面,Git让我们更加与世界接轨,不再是"局域网"的程序猿,特别是掌握了Git之后,会发现它真的很好用,本文对Git中比较烦人的冲突 ...
- 在linux环境编译boost
1.在boost官网:http://www.boost.org/下载相应版本的boost 2.解压boost到相应目录,在boost跟目录下有b2可执行程序,可以通过输入命令“/b2 --help”, ...
- MySQL中的增删改查
将表cm_application中的state字段类型改为字符串型 alter table cm_application modify STATE varchar(50); 将表cm_applic ...
- CentOS搭建svn服务器支持https访问
在CentOS6.3 64位机器上配置SVN服务器,并设置只允许HTTPS连接,可以配置多个repos源,每个源都拥有自己的组和成员,用于权限控制. 安装相关软件 Apache yum install ...
- nyoj756_重建二叉树_先序遍历
重建二叉树 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 题目很简单,给你一棵二叉树的后序和中序序列,求出它的前序序列(So easy!). 输入 输入有多组数 ...
- 【leetcode】House Robber & House Robber II(middle)
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- LeetCode 459 Repeated Substring Pattern
Problem: Given a non-empty string check if it can be constructed by taking a substring of it and app ...