题目说明

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. vim 配置半透明

    转载两个博客 链接一 链接二

  2. EBS-DBA 维护

    --查询表空间使用率: SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB "表空间大小(M)", ...

  3. FreeBSD查看即时网络流量

    1.数据包 “netstat 1″一秒钟累计一次,”netstat 2″两秒钟累计一次.依此类推 2.查看网卡流量:”systat -if 1″每秒钟刷新一次,”systat -if 2″两秒钟刷新一 ...

  4. [C#]SharpSSH-一个可以使用SSH连接的.NET库

    A Secure Shell (SSH) library for .NET 觉得有用,就记录下来了 http://www.tamirgal.com/blog/page/SharpSSH.aspx ht ...

  5. Mongodb 与 SQL 语句对照表

    In addition to the charts that follow, you might want to consider the Frequently Asked Questions sec ...

  6. maven仓库地址配置

    # 背景 maven中央存库在国外,访问缓慢,一般国内镜像,这里推荐阿里云的 http://maven.aliyun.com/nexus/content/groups/public 我之前采用的方式是 ...

  7. Java异常处理机制的秘密

    一.结论 这些结论你可能从未听说过,但其正确性是毋庸置疑的,不妨先看看: 1.catch中throw不一定能抛回到上一层,因为finally中的return会抑制这个throw 2.finally中t ...

  8. 洛谷P4197 Peaks&&克鲁斯卡尔重构树学习笔记(克鲁斯卡尔重构树+主席树)

    传送门 据说离线做法是主席树上树+启发式合并(然而我并不会) 据说bzoj上有强制在线版本只能用克鲁斯卡尔重构树,那就好好讲一下好了 这里先感谢LadyLex大佬的博客->这里 克鲁斯卡尔重构树 ...

  9. Mac 切换到行首和行末的方法

    苹果笔记本没有home键和end键 但是使用 command + 方向键左键可以回到行首, command + 方向键右键可以去到行末

  10. Dynamic Programming-650. 2 Keys Keyboard

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...