题目说明

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,#,#,15,7},

3

/
\

9  20

/  \

15   7

return its level
order traversal as:

[

[3],

[9,20],

[15,7]

]

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

1

/ \

2   3

/

4

\

5

The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

思路

这道题可以这么做:用一个队列来保存每层节点,遍历该层所有节点同时将每个节点加到结果中,同时将每个节点的子女节点用一个list保存下来,遍历完本层节点后,将保存的子女节点加到队列中继续遍历。直到子女节点为空(也就是队列为空)为止

代码

public ArrayList<ArrayList<Integer>> levelOrder(TreeNode root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
ArrayList<ArrayList<Integer>> ans=new ArrayList<ArrayList<Integer>>();
if(root==null)
return ans;
Queue<TreeNode> list=new LinkedList<TreeNode>();
list.add(root);
while(!list.isEmpty())
{
ArrayList<TreeNode> levelNodes=new ArrayList<TreeNode>();
ArrayList<Integer> res=new ArrayList<Integer>();
while(!list.isEmpty())
{
TreeNode node=list.poll();
if(node.left!=null)
levelNodes.add(node.left);
if(node.right!=null)
levelNodes.add(node.right);
res.add(node.val);
}
list.addAll(levelNodes);
ans.add(res);
}
return ans;
}
}

[LeetCode] Level Order Traversal的更多相关文章

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

  2. [LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历

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

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

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

  4. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  5. [LeetCode]题解(python):107 Binary Tree Level Order Traversal II

    题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal-ii/ Given a binary tree, return ...

  6. [LeetCode]题解(python):103 Binary Tree Zigzag Level Order Traversal

    题目来源 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, re ...

  7. [LeetCode]题解(python):102 Binary Tree Level Order Traversal

    题目来源 https://leetcode.com/problems/binary-tree-level-order-traversal/ Given a binary tree, return th ...

  8. Binary Tree Level Order Traversal II——LeetCode

    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. 深入浅析Node.js单线程模型

    Node.js采用 事件驱动 和 异步I/O 的方式,实现了一个单线程.高并发的运行时环境,而单线程就意味着同一时间只能做一件事,那么Node.js如何利用单线程来实现高并发和异步I/O?本文将围绕这 ...

  2. maven下@override标签失效

    经常遇见此问题,现记录如下,以备下次查阅. 在pom文件添加配置: <plugin> <groupId>org.apache.maven.plugins</groupId ...

  3. html.EditorForModel自定义模版

    https://www.cnblogs.com/lori/p/5969658.html  http://www.cnblogs.com/yinzixin/archive/2012/12/18/2821 ...

  4. Elasticsearch 在 windows 和 ubuntu 下详细安装过程

    1. 前言 作为一名 .NET 平台开发者,选择开发框架时总会面临更多的局限性,不过对于搜索这种刚需服务来说,开源框架可供选择的余地还是比较大的.笔者之前用的是 Lucene.net ,现在深感其使用 ...

  5. 设计模式之组合模式(Composite Pattern)

    一.什么是组合模式? 组合模式提供了一种层级结构,并允许我们忽略对象与对象集合之间的差别 调用者并不知道手里的东西是一个对象还是一组对象,不过没关系,在组合模式中,调用者本来就不需要知道这些 二.举个 ...

  6. C# GDI绘制波形图

    直接上效果图如下 public partial class WaveChartUserCtrl : UserControl { Color axisColor = Color.FromArgb(69, ...

  7. H - The LCIS on the Tree HDU - 4718

    The LCIS on the Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Oth ...

  8. clang 编译 OC

    clang -fobjc-arc -framework Foundation helloworld.m -o helloworld.out OVERVIEW: clang LLVM compiler ...

  9. github本地分支合并到线上主分支

    如果是在本地index-swiper分支上,已经写好了那么: 1,git add .             //提交到本地缓冲区 2,git commit -m "project init ...

  10. setInterval传递参数

    参照:https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout 平时我们使用定时器的时 ...