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. 【Hive】元数据库部署的三种方式和选择【metaStore server】

    一.Derby 元数据使用之前,要在hive目录下执行schematool命令,进行初始化设置 bin/schematool -dbType derby -initSchema 启动hive后,可以用 ...

  2. 读后笔记 -- Python 全栈测试开发 Chapter11:Python + Requests 实现接口测试

    11.1 Requests 框架 11.1.1 requests 请求 1. reqeusts 库 安装 pip install requests 2. requests 库 GET 方法,参数通过 ...

  3. (Yocto)Imx8mp的时间结构

    1.构成图 #kernel\time\timekeeping.c #drivers\rtc\class.c 1.time date source 解释       rx8010sj: 自己定制的开发板 ...

  4. EXPORT_SYMBOL的正常使用

    1.EXPORT_SYMBOL的作用是什么? EXPORT_SYMBOL标签内定义的函数或者符号对全部内核代码公开,不用修改内核代码就可以在您的内核模块中直接调用,即使用EXPORT_SYMBOL可以 ...

  5. 【vscode】linux下vscode的使用

    注1:vscode在查看project时,非常好用,可以导入整个project并查看其中文件,通过插件的安装还可以实现跳转到当前函数定义处的功能; 注2:可以了解下source insight; 补充 ...

  6. Unity 读取Json文件、创建Json文件

    using System.IO; using UnityEngine; public class ReadJson:MonoBehaviour { public static TestSetting ...

  7. 华为交换机,改vlan的方法

    telnet 登录1.1.1.111, 如果登录再到其他交换机,需要在用户状态,telnet其他的ip.不能在系统用户状态下跳转登录. sys改为系统用户  状态符由尖括号,改为方括号 display ...

  8. VS2017创建Linux项目实现远程GDB调试

    vs2017新增linux for C++的模块,尝试安装了一下环境. 首先,安装VS2017,安装时注意选择以下模块: 安装完成后,需要配置Linux服务端的部分,我的配置过程如下: 第一步,安装V ...

  9. select控件操作汇总

    1.通过select的text来选中对应的option $("#dddddd option:contains('小型车')").attr("selected", ...

  10. Spring UnitTest

    demo:https://files.cnblogs.com/files/netact/spring01.zip 首先说一下Spring IOC的运行机制,同过xml或者annotation()来将p ...