102. 二叉树的层次遍历

102. Binary Tree Level Order Traversal

题目描述

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

每日一算法2019/5/11Day 8LeetCode102. Binary Tree Level Order Traversal

例如:

给定二叉树: [3,9,20,null,null,15,7],

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

返回其层次遍历结果:

  1. [
  2. [3],
  3. [9,20],
  4. [15,7]
  5. ]

Java 实现

  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import java.util.Queue;
  4. class TreeNode {
  5. int val;
  6. TreeNode left;
  7. TreeNode right;
  8. TreeNode(int x) {
  9. val = x;
  10. }
  11. }
  12. class Solution {
  13. public List<List<Integer>> levelOrder(TreeNode root) {
  14. List<List<Integer>> result = new LinkedList<>();
  15. Queue<TreeNode> queue = new LinkedList<>();
  16. if (root == null) {
  17. return result;
  18. }
  19. queue.offer(root);
  20. while (!queue.isEmpty()) {
  21. int size = queue.size();
  22. List<Integer> list = new LinkedList<>();
  23. for (int i = 0; i < size; i++) {
  24. if (queue.peek().left != null) {
  25. queue.offer(queue.peek().left);
  26. }
  27. if (queue.peek().right != null) {
  28. queue.offer(queue.peek().right);
  29. }
  30. list.add(queue.poll().val);
  31. }
  32. result.add(list);
  33. }
  34. return result;
  35. }
  36. }

相似题目

参考资料

LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8的更多相关文章

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

  2. 二叉树的层次遍历 · Binary Tree Level Order Traversal

    [抄题]: 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) [思维问题]: [一句话思路]: 用queue存每一层 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况 ...

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

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

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

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

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

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

  7. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

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

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

随机推荐

  1. Flask一种通用视图,增删改查RESTful API的设计

    模型设计是后端开发的第一步.数据模型反映了各种对象之间的相互关系. from app import db class Role(db.Model): """角色" ...

  2. C仿黑白棋版XO棋

    两位玩家轮流在棋盘上放置不同颜色的棋子,一位玩家使用黑子,另一位使用白子,棋盘是一个偶数正方形. 只能将一个棋子放在对手的棋子旁边,使对手在水平.垂直.对角线方向上的棋子变成自己的棋子,游戏结束时,棋 ...

  3. env (arcpy)

    addOutputsToMap (读写) 设置是否应将工具产生的输出数据集添加至应用程序显示. Boolean autoCommit (读写) 支持“自动提交”环境的工具将在 ArcSDE 事务中进行 ...

  4. SetThreadAffinityMask windows下绑定线程(进程)到指定的CPU核心

    原帖地址:https://www.cnblogs.com/lvdongjie/p/4476766.html 一个程序指定到单独一个CPU上运行会比不指定CPU运行时快.这中间主要有两个原因:1)CPU ...

  5. JVM 自定义类加载器

    一.创建自定义类加载器 package com.example.jvm.classloader; import java.io.ByteArrayOutputStream; import java.i ...

  6. Jenkins入门【转】

    一.Jenkins概述 二.安装Jenkins https://pkg.jenkins.io/redhat-stable/ sudo wget -O /etc/yum.repos.d/jenkins. ...

  7. Perf -- Linux下的系统性能调优工具,第 1 部分 应用程序调优的使用和示例 Tracepoint 是散落在内核源代码中的一些 hook,一旦使能,它们便可以在特定的代码被运行到时被触发,这一特性可以被各种 trace/debug 工具所使用。Perf 就是该特性的用户之一。

    Perf -- Linux下的系统性能调优工具,第 1 部分 应用程序调优的使用和示例 https://www.ibm.com/developerworks/cn/linux/l-cn-perf1/i ...

  8. TextFX Notepad++

    textFx Notepad++ - 国内版 Bing https://cn.bing.com/search?FORM=U227DF&PC=U227&q=textFx+Notepad% ...

  9. 泡泡一分钟: A Linear Least Square Initialization Method for 3D Pose Graph Optimization Problem

    张宁 A Linear Least Square Initialization Method for 3D Pose Graph Optimization Problem "链接:https ...

  10. maven 引入的jar有出现两种图标

    两种同样都引入到maven项目中,但是第二种在打包的过程中会显示找不到jar,无法调用!