[抄题]:

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

[思维问题]:

[一句话思路]:

用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. Windows 8的用户模式Shim Engine小探及利用

    转载: https://bbs.pediy.com/thread-175483.htm Windows Shim Engine,即Windows 兼容性模式实现引擎,在exe文件的属性对话框中有一个兼 ...

  2. SDL播放音频的时候发现SDL_OpenAudioDevice打开一直失败

    1:在使用SDL播放音频的时候发现SDL_OpenAudioDevice打开一直失败,导致SDL不能进入回调函数. 使用SDL_GetError()打印错误提示XAudio2: XAudio2Crea ...

  3. Mybatis通过colliection属性递归获取菜单树

    1.现有商品分类数据表category结构如下,三个字段都为varchar类型 2.创建商品分类对应的数据Bean /** * */ package com.xdw.dao; import java. ...

  4. javaScript中对象属性的访问

    有两种方式访问对象属性,一个是点操作符(.),一种是中括号操作符([ ]). 当你知道属性的名称时,使用点操作符: var myObj = { prop1: "val1", pro ...

  5. 查看linux服务器CPU数量

    首先,要区分两个概念:物理CPU和逻辑CPU. 物理CPU就是服务器上实际安装的CPU.但是一个物理CPU可以有多个核.例如,一个 i5 760 是双核,而一个 i5 2250 是四核.如果开启了In ...

  6. MySql 链接字符串

    MySql连接字符串总结 1.本地数据库连接    <connectionStrings>        <add name="ConnectionString" ...

  7. Linux运维面试贩卖思路如下

    1.自我介绍 2.技术介绍 3.上家公司情况介绍.多少人的团队.运维多少人.多少设备.公司什么业务.访问量多少.并发多少.架构多大,然后介绍公司架构.CDN->负载均衡->web-> ...

  8. (4/24) webpack3.x快速搭建本地服务和实现热更新

    写在前面: (1)为了防止版本兼容问题,此处的webpack版本与之前的一致为:webpack@3.6.0.同时这里我们安装的webpack-dev-server版本是2.9.7版本. (2)之前已经 ...

  9. FireDACQuery FDQuery New

    FDQuery FDQuery1->ChangeCount;也有UpdatesPending属性 FDQuery1->ApplyUpdates() ExecSQL('select * fr ...

  10. 使用 C++11 编写类似 QT 的信号槽——上篇

    了解 QT 的应该知道,QT 有一个信号槽 Singla-Slot 这样的东西.信号槽是 QT 的核心机制,用来替代函数指针,将不相关的对象绑定在一起,实现对象间的通信. 考虑为 Simple2D 添 ...