[抄题]:

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

[思维问题]:

[一句话思路]:

用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. 如何捕捉@tornado.gen.coroutine里的异常

    from tornado import gen from tornado.ioloop import IOLoop @gen.coroutine def throw(a,b): try: a/b ra ...

  2. [datatable]借助DataTable的Compute方法

    借助DataTable的Compute方法,DataTable中数据不用事先排好序. 下面代码中的dt是跟前面的是一样的 DataTable dtName = dt.DefaultView.ToTab ...

  3. javascript创建对象之构造函数模式(二)

    对上一章节的工厂模式进行代码重写 function Human(name, sex) { this.name = name; this.sex = sex; this.say = function ( ...

  4. MySQL 创建数据库的两种方法

    使用 mysqladmin 创建数据库 使用普通用户,你可能需要特定的权限来创建或者删除 MySQL 数据库. 所以我们这边使用root用户登录,root用户拥有最高权限,可以使用 mysql mys ...

  5. 深度学习RNN实现股票预测实战(附数据、代码)

    背景知识 最近再看一些量化交易相关的材料,偶然在网上看到了一个关于用RNN实现股票预测的文章,出于好奇心把文章中介绍的代码在本地跑了一遍,发现可以work.于是就花了两个晚上的时间学习了下代码,顺便把 ...

  6. async task 异步消息

     async 和 await 是用来定义的异步方法,async  关键字是上下文关键字,原因在于只有当它修饰方法.lambda 表达式或匿名方法时,它才是关键字. 在所有其他上下文中,都会将其解释为标 ...

  7. redis实现分布式锁 转自importnew 记录一下

    前言 分布式锁一般有三种实现方式:1. 数据库乐观锁:2. 基于Redis的分布式锁:3. 基于ZooKeeper的分布式锁.本篇博客将介绍第二种方式,基于Redis实现分布式锁.虽然网上已经有各种介 ...

  8. tornado-简单的服务器非阻塞

    1.服务器 非阻塞 import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.op ...

  9. oracle库和表空间

    完整的Oracle数据库通常由两部分组成:Oracle数据库和数据库实例. 1) 数据库是一系列物理文件的集合(数据文件,控制文件,联机日志,参数文件等): 2) Oracle数据库实例则是一组Ora ...

  10. 使用phpExcelReader操作excel提示The filename *.xls is not readable的详细解决方法

    使用phpExcelReader操作excel提示The filename *.xls is not readable的详细解决方法 是xls文件有问题,另存为新的xls文件,然后导入就不会有这个问题