[抄题]:

给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问)

[思维问题]:

[一句话思路]:

用queue存每一层

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 每一层level存的东西都是不一样的,所以要在queue非空的循环中再新建。不要当成全局变量。
  2. root为空时,返回result空数组,而不是null四个字母
  3. stack用pop 弹出来,queue用poll 拉出来,想想也比较形象。root非空用null,数据结构非空要用isEmpty()函数

[二刷]:

  1. 不要忘了写特判

[三刷]:

[四刷]:

[五刷]:

[总结]:

把size单独设成变量 ,每次都取一遍 多取几次总不会错 (否则取左、右会导致queue.size()很大,level会多加)

树的BFS不会重复,所以不需要用哈希表。只要用queue即可。

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[英文数据结构或算法,为什么不用别的数据结构或算法]:

  1. 用queue存每一层,实现先进先出
  2. 注意格式:接口 名 = new 具体实现;eg 队列可以用静态数组、链表linked list来实现
  3. List<List<Integer>> result = new ArrayList<List<Integer>>(); list里面装list,效果是一层一层的:
[
[3],
[9,20],
[15,7]
]

[其他解法]:

2queue,dummy node

[Follow Up]:

  1. 把结果反过来写:用Collections.reverse(result);函数
  2. DFS:迭代式的深度优先搜索,不断放宽MAX_LEVEL条件,直到某层没有点。(历史上,内存有限的时候用)

[LC给出的题目变变变]:

637. Average of Levels in Binary Tree

103. Binary Tree Zigzag Level Order Traversal

public class Solution {
/*
* @param root: A Tree
* @return: Level order a list of lists of integer
*/
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
Queue<TreeNode> queue = new LinkedList<TreeNode>(); if (root == null) {
return result;
} queue.offer(root);
while (!queue.isEmpty()) {//
int size = queue.size();
List<Integer> level = new ArrayList<Integer>();
for (int i = 0; i < size; i++) {
TreeNode curt = queue.poll();//
level.add(curt.val);
if (curt.left != null) {
queue.offer(curt.left);
}
if (curt.right != null) {
queue.offer(curt.right);
}
}
result.add(level);
}
return result;
}
}

二叉树的层次遍历 · Binary Tree Level Order Traversal的更多相关文章

  1. LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8

    102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...

  2. [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, ...

  3. (二叉树 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 ...

  4. 二叉树叶子顺序遍历 · binary tree leaves order traversal

    [抄题]: 给定一个二叉树,像这样收集树节点:收集并移除所有叶子,重复,直到树为空. 给出一个二叉树: 1 / \ 2 3 / \ 4 5 返回 [[4, 5, 3], [2], [1]]. [暴力解 ...

  5. [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)

    描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...

  6. [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 ...

  7. [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 ...

  8. [LeetCode] Binary Tree Level Order Traversal 二叉树层序遍历

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  9. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

随机推荐

  1. 搭建OpenStack先电云平台

    实际操作示意图 在VMware里面创建两台centos7的虚拟机作为搭建云平台的两节点配置如下: 1.第一台虚拟机   作为控制节点 2CPU 3G以上内存 硬盘50G 网络适配器一个nat 一个仅主 ...

  2. linux中的ftp命令

    转载至:https://www.cnblogs.com/mingforyou/p/4103022.html 一.ftp的get命令和mget命令有何不同? get一次只下载一个文件:mget一次可以下 ...

  3. UMG设置组件自适应居中或靠边

    转自:http://aigo.iteye.com/blog/2297430 比如你的UI中有些组件需要居中显示,有些需要始终在右上角并且与上边框和右边框保持固定的距离等等,对于这种需要动态适应屏幕分辨 ...

  4. SQL查询日期:

    SQL查询日期: 今天的所有数据:select * from 表名 where DateDiff(dd,datetime类型字段,getdate())=0 昨天的所有数据:select * from ...

  5. Windows10右键添加“在此处打开命令窗口"

    今天跑代码时用到了Powershell,有一个环境变量死活都找不到,无奈只好重新回到cmd命令行测试,结果立马就跑通了.-_-||  由于现在Win10默认右键只有Powershell,所以为了以后方 ...

  6. 在docker中运行kong和kong dashboard

    一.制作alpine版的kong镜像 https://github.com/Kong/docker-kong/tree/d4cec3dc46c780a916a40963309554ca81da2b46 ...

  7. Nature | 光学CNN层替换传统CNN层,超省电

    CNN 计算效率的研究一直备受关注,但由于功率和带宽的严格限制,CNN 仍难以应用在嵌入式系统如移动视觉.自动驾驶中.在斯坦福大学发表在 Nature 旗下 Scientific Reports 的这 ...

  8. serializers 序列化器里面进行 校验等

    一.第一版(一般不用) # 声明序列化器from rest_framework import serializersfrom djangoDome.models import Book class P ...

  9. Python之模块(一)

    模块 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码就相对较少 ...

  10. 系统一般信息监控查看shell.磁盘,负载等达阀值告警机制,改进测试中.

    1 #!/bin/sh  2 #Create by Qrui  3 while [ "1"="1" ]  4 do  5 clear  6  7 echo &q ...