Get the list of list of keys in a given binary tree layer by layer. Each layer is represented by a list of keys and the keys are traversed from left to right.

Examples

5

/    \

3        8

/   \        \

1     4        11

the result is [ [5], [3, 8], [1, 4, 11] ]

Corner Cases

  • What if the binary tree is null? Return an empty list of list in this case.

How is the binary tree represented?

We use the level order traversal sequence with a special symbol "#" denoting the null node.

For Example:

The sequence [1, 2, 3, #, #, 4] represents the following binary tree:

1

/   \

2     3

/

4

/**
* public class TreeNode {
* public int key;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int key) {
* this.key = key;
* }
* }
*/
public class Solution {
public List<List<Integer>> layerByLayer(TreeNode root) {
// Write your solution here
// use BFS to solve this, thus, we use queue to maintain the nodes that we have seen but haven't deal with,
// and for each layer, we maintain a List to store all keys in this layer
// the tree can be null, then we return a List contains nothing
// the number of the elements in a single layer is less than Integer.MAX_VALUE
List<List<Integer>> res = new ArrayList<>();
if(root==null){
return res;
}
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
while(q.size()>0){
int size = q.size();
List<Integer> layer = new ArrayList<>();
for(int i=0; i<size; i++){
TreeNode cur = q.poll();
if(cur!=null){
layer.add(cur.key);
}
if(cur.left!=null){
q.offer(cur.left);
}
if(cur.right!=null){
q.offer(cur.right);
}
}
res.add(layer);
}
return res;
}
}

Get Keys In Binary Tree Layer By Layer的更多相关文章

  1. PAT 2019-3 7-4 Structure of a Binary Tree

    Description: Suppose that all the keys in a binary tree are distinct positive integers. Given the po ...

  2. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  3. Binary Tree Zigzag Level Order Traversal [LeetCode]

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

  4. LintCode 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 | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  6. 814. Binary Tree Pruning(leetcode) (tree traverse)

    https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...

  7. leetcode_919. Complete Binary Tree Inserter_完全二叉树插入

    https://leetcode.com/problems/complete-binary-tree-inserter/ 给出树节点的定义和完全二叉树插入器类的定义,为这个类补全功能.完全二叉树的定义 ...

  8. What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?

    Binary Tree : It is a tree data structure in which each node has at most two children. As such there ...

  9. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  10. [Swift]LeetCode637. 二叉树的层平均值 | Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

随机推荐

  1. Tomcat集群配置--负载均衡

    Tomcat集群配置学习篇-----分布式应用 现目前基于javaWeb开发的应用系统已经比比皆是,尤其是电子商务网站,要想网站发展壮大,那么必然就得能够承受住庞大的网站访问量:大家知道如果服务器访问 ...

  2. vue 添加代理

    1.跟目录下新建vue.config.js文件,添加内容: module.exports = {   assetsDir: 'static',   parallel: false,   publicP ...

  3. vue打包添加时间戳,实现更新项目自动清除缓存

    本来vue打包会自动用chunkhash来解决缓存问题,但是部分浏览器不会自动更新,因此可以通过后面t=${ }的不同来实现自动重新加载文件,保持最新的界面 (1).webpack打包:修改build ...

  4. CentOS 6.7 hadoop free版本Spark 1.6安装与使用

    最近的工作主要围绕文本分类,当前的解决方案是用R语言清洗数据,用tm包生成bag of words,用libsvm与liblinear训练模型.这个方案可以hold住6/70万的训练集: LIBLIN ...

  5. Vue+element ui 笔记

    1)可以直接拿过来就用的样式 https://www.cnblogs.com/xiao987334176/p/14188413.html 2)对Table里面的每一项全部设定为选中 mounted() ...

  6. TODO留学小程序,展开,收起失效

    text设置user-select=true后,display: -webkit-box 失效? https://developers.weixin.qq.com/community/develop/ ...

  7. import cv2时出现ImportError: DLL load fail:找不到指定模块

  8. unity002

    物体轴心点变换 轴向变换 预览 暂停 逐帧播放 坐标 世界坐标 物体坐标 mesh 网格决定形状 渲染

  9. sql server 与mysql差异(innodb)

    MySQL SQL SERVER CHAR_LENGTH(str) LEN(character_expression) CONCAT(str1,str2,...) str1+str2 INSERT(s ...

  10. SAP生产订单没有目标成本的原因解释

    首先,OKV6察看一下目标成本的配置,默认是当期成本估算,见下图: 其次,没有目标成本的原因还可能是下列原因导致: 1.该物料没有成本估算和发布2.工艺路线维护日期晚于这个物料估算日期3.没有做CO1 ...