LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8
102. 二叉树的层次遍历
102. Binary Tree Level Order Traversal
题目描述
给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。
每日一算法2019/5/11Day 8LeetCode102. Binary Tree Level Order Traversal
例如:
给定二叉树: [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
返回其层次遍历结果:
[
[3],
[9,20],
[15,7]
]
Java 实现
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new LinkedList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root == null) {
return result;
}
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> list = new LinkedList<>();
for (int i = 0; i < size; i++) {
if (queue.peek().left != null) {
queue.offer(queue.peek().left);
}
if (queue.peek().right != null) {
queue.offer(queue.peek().right);
}
list.add(queue.poll().val);
}
result.add(list);
}
return result;
}
}
相似题目
参考资料
- https://leetcode.com/problems/binary-tree-level-order-traversal/
- https://leetcode-cn.com/problems/binary-tree-level-order-traversal/
LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8的更多相关文章
- [Swift]LeetCode102. 二叉树的层次遍历 | Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 二叉树的层次遍历 · Binary Tree Level Order Traversal
[抄题]: 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) [思维问题]: [一句话思路]: 用queue存每一层 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况 ...
- 二叉树叶子顺序遍历 · binary tree leaves order traversal
[抄题]: 给定一个二叉树,像这样收集树节点:收集并移除所有叶子,重复,直到树为空. 给出一个二叉树: 1 / \ 2 3 / \ 4 5 返回 [[4, 5, 3], [2], [1]]. [暴力解 ...
- [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] 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 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 二叉树层序遍历 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 II 二叉树层序遍历之二
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- golang-os文件操作
golang-os文件操作 package main import ( "fmt" "os" ) //func main() { // f,err :=os.C ...
- 深入理解JVM虚拟机12:JVM性能管理神器VisualVM介绍与实战
一.VisualVM是什么? VisualVM是一款免费的JAVA虚拟机图形化监控分析工具. 1. 拥有图形化的监控界面. 2. 提供本地.远程的JVM监控分析功能. 3. 是一款免费的 ...
- React Hooks介绍和环境搭建(一)
React Hooks 简介 2018年底FaceBook的React小组推出Hooks以来,所有的React的开发者都对它大为赞赏.React Hooks就是用函数的形式代替原来的继承类的形式,并且 ...
- CentOS 7.2 基于Docker实现MySQL主从架构
原文地址:https://blog.csdn.net/sunnyfg/article/details/80655823 1.安装Docker(略) Centos7下安装Docker : https:/ ...
- 每个Web开发者都需要具备的9个软技能--ZT
本文原始链接:http://www.cnblogs.com/oooweb/p/soft-skills-every-web-developer-should-master.html 对于一份工作,你可能 ...
- ActiveMQ参数异常 “Invalid broker URI”
某次启动项目报错,提示ActiveMQ参数异常 该参数的值配置如下 跟踪读取配置的代码如下,可以看到读取我配置的key为xmq.actmq.connection.url.forSend的对应值,赋值到 ...
- To avoid slowing down lookups on a near-full table, we resize the table when it's USABLE_FRACTION (currently two-thirds) full.
https://github.com/python/cpython/blob/3.8/Objects/dictobject.c
- Oracle列信息表 all_tab_columns中的data_length和data_precision字段区别
Oracle列信息表 all_tab_columns中的data_length和data_precision字段区别 区别: 这两个属性都属于user_tab_columns视图,他们的含义:1,da ...
- tengine无法解析ssi报错 Nginx: unsafe URI detected while sending response
Nginx: unsafe URI detected while sending response 现象:# 类似 <!--#include virtual="../library/h ...
- 【转载】 导入GoogleClusterData到MySQL
原文地址: https://www.cnblogs.com/instant7/p/4159022.html ---------------------------------------------- ...