题目说明

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. hdu 5046 二分+DLX模板

    http://acm.hdu.edu.cn/showproblem.php?pid=5046 n城市建k机场使得,是每个城市最近机场的距离的最大值最小化 二分+DLX 模板题 #include < ...

  2. 【转】WinRT 中SystemTrigger 构造函数的 SystemTriggerType 参数的解释

    中文版:http://msdn.microsoft.com/library/windows/apps/windows.applicationmodel.background.systemtrigger ...

  3. easyui-combobox url绑定后台json数据问题

    <input id="line" name="line" style="max-width:120px;" class="e ...

  4. 关于ListBox的几个问题

    Winfrom ListBox绑定数据源list界面不更新问题与绑定数据源不可CRUD问题 场景:获取一个listbox的选中项添加到另一个listbox中 解决方案-1:不要直接绑定DataSour ...

  5. MVVM前端框架

    早开始接触MVVM框架的时候,是在学习WPF的时候,后面陆陆续续接触到了很多的前端JS框架,个人觉得大同小异,也没有去研究源代码,所以都停留在使用的阶段.当然对于我来说,使用这些JS框架,最关注的无非 ...

  6. Easyui datagrid绑定数据,新增,修改,删除写法

    @{ ViewBag.Title = "xw_xsfl"; } <script type="text/javascript"> var editIn ...

  7. RabbitMQ之消息持久化(转)

    原文地址 https://blog.csdn.net/u013256816/article/details/60875666/ 消息的可靠性是RabbitMQ的一大特色,那么RabbitMQ是如何保证 ...

  8. 四两拨千斤式的攻击!如何应对Memcache服务器漏洞所带来的DDoS攻击?

    本文由  网易云发布. 近日,媒体曝光Memcache服务器一个漏洞,犯罪分子可利用Memcache服务器通过非常少的计算资源发动超大规模的DDoS攻击.该漏洞是Memcache开发人员对UDP协议支 ...

  9. 【UVA11107 训练指南】Life Forms【后缀数组】

    题意 输入n(n<=100)个字符串,每个字符串长度<=1000,你的任务是找出一个最长的字符串使得超过一半的字符串都包含这个字符串. 分析 训练指南上后缀数组的一道例题,据说很经典(估计 ...

  10. 洛谷P3369 【模板】普通平衡树(Splay)

    题面 传送门 题解 鉴于最近的码力实在是弱到了一个境界--回来重新打一下Splay的板子--竟然整整调了一个上午-- //minamoto #include<bits/stdc++.h> ...