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. HelloWorld (用记事本写,在dos窗口里运行)

    编写HelloWorld (用记事本写,在dos窗口里运行) 首先在任何一个盘中创建一个文件夹 在文件夹里新建一个HelloWorld.java文件,注意后缀名是.java(将文件拓展名打开) 打开这 ...

  2. Android 杂项

    1. String to InputStream new ByteArrayInputStream(str.getBytes());

  3. 【驱动 】frambuffer中显示屏参数的修改

    1.在x210板子的kernel中,默认LCD显示屏是800*400的,修改在 kernel/arch/arm/mach-s5pv210/mach-x210.c 中 258行 #define S5PV ...

  4. pytorch 入门

    import matplotlib.pyplot as plt from torchvision.transforms import ToTensor import torch from torch ...

  5. SQL_TIP_JOIN_x

    没有条件的JOIN会导致数据数量变为两表的数据量的乘积结果. 用ON来在这些结果里进行筛选 on T1.A = T2.A的时候,如果T1的A是不重复的,则实际上是在对T2现有数据做筛选,结果数据量&l ...

  6. Jenkins安装和自动化部署

    1.Jenkins安装机器安装要求可以参考官网 https://www.jenkins.io 2.下载jenkins的war包上传到linux上部署 3.安装jdk.git.maven 3.1.安装j ...

  7. JS基础笔记汇总

    JS基础笔记最全的汇总 javascript介绍以及起源目录1.变量和常量的知识2.基本数据类型3.运算符4.基本数据类型间的转换5.流程控制语句 一.javascript介绍以及起源 js一种直译型 ...

  8. IBM服务器的2种IMM和1种raid管理方式

    IMM两种进入方式和Raid管理软件 前面板IMM管理接口:     用网线连接服务器前置面板的管理接口到其他 PC 或笔记本,然后 PC 或笔记本的 IP 地址配置为 192.168.70.0/24 ...

  9. java的排序问题

    普通排序 对于基础数据类型的排序,基本只是调用一下方法 如java的 1 Arrays.sort(nums); 那么如何自定义排序规则呢? 自定义排序规则: 假设现在有这么个问题,有n个学生, 每个学 ...

  10. C++的右值引用是左值,rvalue reference is lvalue.

    参考: https://stackoverflow.com/questions/28483250/rvalue-reference-is-treated-as-an-lvalue