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. 模拟ATM系统 —— 用户注册、登录和用户操作页设计 、查询账号、退出账号功能

    开发工具:idea 一.项目步骤: 1.系统准备.首页设计 2.用户开户功能 3.用户登录功能 4.用户操作页设计 .查询账号.退出账号功能 5.用户存款功能 6.用户取款功能 7.用户转账功能 8. ...

  2. Windows10下SecureCRT、SecureFX安装与破解(超级详细)

    整理了Windows10下最新版本SecureCRT9.1.SecureFX9.1安装 1.资源地址: 链接:https://pan.baidu.com/s/1XoQqpRlpBm6Tvc0fHni6 ...

  3. Chart控件-常用设置

    visual studio中原生控件chart控件使用时的一些常用设置 鼠标缩放功能 缩放后恢复曲线

  4. mblink study111

    mblink study  https://gitee.com/dotnetchina/NanUIhttps://blog.csdn.net/u012814856/article/details/70 ...

  5. demo code

    using System.Reflection; // 引用这个才能使用Missing字段 namespace hello{    public partial class Form1 : Form  ...

  6. Net Core 3.1 ONVIF 操控海康摄像头

    先给出实现的代码 https://github.com/lu1770/onvif-client.git 也可以通过安装包来使用功能 dotnet add package Onvif 基本用法 Agen ...

  7. 谷歌Chrome浏览器网页中看视频出现绿屏、闪烁和花屏等显示问题解决方法

    方法一(推荐): 1.在chrome地址栏输入chrome://flags/2.搜索Hardware-accelerated video encode把Enabled改成Disabled 3.搜索Ha ...

  8. ES bool查询

    一.bool查询包含四种操作 1.must 2.must not 3.filter 4.should 二.功能 1.must 对应mysql的 and a= 2.must not 对应mysql的 a ...

  9. sql使用!=查询时会忽略null数据

    table_a有3条数据 column1值分别为1,0,null 那么 select * from table_a where column1!='1' 只会查到clumn1为0的数据,null的数据 ...

  10. PCA降维练习

    [题目] 1.现有我国大陆30个省.直辖市.自治区的经济发展状况数据集如表所示,包括8项经济指标:国民生产总值(A1):居民消费水平(A2):固定资产投资(A3):职工平均工资(A4):货物周转量(A ...