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

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
/ \
9 20
/ \
15 7

return its level order traversal as:

[
[3],
[9,20],
[15,7]
]

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

使用队列存储每一层的节点,因为队列是先进先出的。进入循环弹出这一层节点存入数组,把弹出的节点的左右子节点存入队列中,依次循环。

需要注意的几点:

(1)队列中使用offer方法添加元素,如果发现队列已满无法添加元素的话,会直接返回false。

如果用add方法添加元素,若超出了队列长度会直接抛异常。

(2)队列中使用poll取出并移除头元素,如果队列为空,返回null。如果食用remove,若队列为空,抛出NoSuchElementException异常。

(3)判断队列是否为空queue.isEmpty( ).

(4)新建队列 Queue<TreeNode> queue = new LinkedList<TreeNode>( );

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
ArrayList result = new ArrayList();
if (root == null) {
return result;
} Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while (!queue.isEmpty()) {
ArrayList<Integer> level = new ArrayList<Integer>();
int size = queue.size();
for(int i = 0; i < size; i++) {
TreeNode node = queue.poll();
level.add(node.val);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
result.add(level);
}
return result;
}
}
 

leetcode 102. Binary Tree Level Order Traversal的更多相关文章

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

  2. leetcode 102 Binary Tree Level Order Traversal ----- java

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

  3. Java [Leetcode 102]Binary Tree Level Order Traversal

    题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  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 102. Binary Tree Level Order Traversal 二叉树的层次遍历 C++

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

  6. Java for 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, ...

  7. leetcode 102 Binary Tree Level Order Traversal(DFS||BFS)

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

  8. Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS

    二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  9. leetcode 102.Binary Tree Level Order Traversal 二叉树的层次遍历

    基础为用队列实现二叉树的层序遍历,本题变体是分别存储某一层的元素,那么只要知道,每一层的元素都是上一层的子元素,那么只要在while循环里面加个for循环,将当前队列的值(即本层元素)全部访问后再执行 ...

随机推荐

  1. json转换成对象

    在json转换成对象时,json的key会与java 类的字段一一对应.如果没有映射上的java字段会在该数据类型上填充默认值,如int 0,String null 等. 没有映射的json key在 ...

  2. 【原】http缓存与cdn相关技术

    摘要:最近要做这个主题的组内分享,所以准备了一个星期,查了比较多的资料.准备的过程虽然很烦很耗时间,不过因为需要查很多的资料,因此整个过程下来,对这方面的知识影响更加深刻.来来来,接下来总结总结 一 ...

  3. sql lock

    要提升SQL的查询效能,一般来说大家会以建立索引(index)为第一考虑.其实除了index的建立之外,当我们在下SQL Command时,在语法中加一段WITH (NOLOCK)可以改善在线大量查询 ...

  4. matlab解三元二次方程组

    C1=7.0863; C2=6.8971; C3=0.4929; C4=0.8131; C5=1.8240; C6=3.8108; C7=3.7318; C8=-2.2238; C9=1.9905; ...

  5. /MT、/MD编译选项,以及可能引起在不同堆中申请、释放内存的问题

    一.MD(d).MT(d)编译选项的区别 1.编译选项的位置 以VS2005为例,这样子打开: 1)         打开项目的Property Pages对话框 2)         点击左侧C/C ...

  6. Android学习笔记——Bundle

    该工程的功能是实现不同线程之间数据的传递 以下代码是MainActivity.java中的代码 package com.example.bundle; import android.app.Activ ...

  7. Visual Studio的调试技巧

    Visual Studio的调试技巧 [原文地址] Debugging Tips with Visual Studio 2010 [原文发表日期] 2010/8/19 10:48 AM 这是我写的关于 ...

  8. Python之路【第五篇续】:面向对象编程二

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABgQAAALaCAIAAABxja8cAAAgAElEQVR4nOzd6X9Tdd74/+uv+f5uzF

  9. 将excel文件中的数据导入到mysql

    ·在你的表格中增加一列,利用excel的公式自动生成sql语句,具体方法如下:          1)增加一列(假设是D列)          2)在第一行的D列,就是D1中输入公式:=CONCATE ...

  10. php中的正则函数主要有三个-正则匹配,正则替换

    php中变量的声明? 由于php声明变量的时候, 不支持使用 var关键字, 又不能直接写一个变量名字, 孤零零的放在那里, 所以, 在php中声明变量的方式, 同时也是给变量初始化的形式, 即: & ...