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]
]

解题思路:

前序遍历(BFS),使用一个Queue存储每层元素即可,JAVA实现如下:

    public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if (root == null)
return list;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.add(root);
while (queue.size() != 0) {
List<Integer> alist = new ArrayList<Integer>();
for (TreeNode child : queue)
alist.add(child.val);
list.add(new ArrayList<Integer>(alist));
Queue<TreeNode> queue2=queue;
queue=new LinkedList<TreeNode>();
for(TreeNode child:queue2){
if (child.left != null)
queue.add(child.left);
if (child.right != null)
queue.add(child.right);
}
}
return list;
}

Java for LeetCode 102 Binary Tree Level Order Traversal的更多相关文章

  1. Java for LeetCode 107 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] 102. Binary Tree Level Order Traversal 二叉树层序遍历

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

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

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

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

  6. 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 二叉树的层次遍历 C++

    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(DFS||BFS)

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

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

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

随机推荐

  1. HDU1421

    提交啦n次一直WA,这个bug找啦几个小时,最终才发现数组开小啦,真是遗憾.这是一个典型的DP问题,题目要求从n个中选出k对使得最终疲劳度最小.首先对物品质量a[n]进行一次排序,用dp[i][j]表 ...

  2. ImportError: cannot import name patterns

    The use of patterns is deprecated in Django1.10. Therefore do not import 'patterns' and your url pat ...

  3. Linux学习之十四-Linux文件和目录权限

    Linux文件和目录权限 在Linux中的每一个文件或目录都包含有访问权限,这些访问权限决定了谁能访问和如何访问这些文件和目录. 通过设定权限可以从以下三种访问方式限制访问权限:只允许用户自己访问:允 ...

  4. mootools客户端框架

    mootools客户端框架 学习:http://www.chinamootools.com/ 官网:https://mootools.net/ 下载地址: https://github.com/moo ...

  5. winform中tabControl的标题隐藏

    方法一: Apperarance 属性:Faltbuttons SizeMode属性:Fixed 各个TabPage的Text :空 ItemSize : Width=0;Height=1; //He ...

  6. Eclipse Memory Analyzer安装

    转载:http://www.jianshu.com/p/3b3c3a914724 1.下载地址:Eclipse Memory Analyzer Open Source Project   2.点击进入 ...

  7. LeetCode Search in Rotated Sorted Array II -- 有重复的旋转序列搜索

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  8. apue学习笔记(第五章 标准I/O)

    本章讲述标准I/O库 流和FILE对象 对于标准I/O库,它们的操作是围绕流进行的.流的定向决定了所读.写的字符是单字节还是多字节的. #include <stdio.h> #includ ...

  9. vscode 右键文件或者文件夹显示菜单

    1.这个是可以在安装时直接选择显示的,如果跟我一样没有选也不愿意重新安装的,可以复制下面代码保存为vsCodeOpenFolder.reg,红色部分是vscode安装路径,换成自己本地路径即可. 双击 ...

  10. IE对CSS样式的数量和大小的限制

    项目中遇到的问题,css写的样式无法渲染,各种百度后发现大概是这个原因: IE对CSS样式的数量和大小的限制 文档中只有前31个link或style标记关联的CSS能够应用. 从第32个开始,其标记关 ...