Get Keys In Binary Tree Layer By Layer
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的更多相关文章
- 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 ...
- leetcode 111 minimum depth of binary tree
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- 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 ...
- LintCode Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 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 ...
- 814. Binary Tree Pruning(leetcode) (tree traverse)
https://leetcode.com/contest/weekly-contest-79/problems/binary-tree-pruning/ -- 814 from leetcode tr ...
- leetcode_919. Complete Binary Tree Inserter_完全二叉树插入
https://leetcode.com/problems/complete-binary-tree-inserter/ 给出树节点的定义和完全二叉树插入器类的定义,为这个类补全功能.完全二叉树的定义 ...
- 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 ...
- 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 ...
- [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 ...
随机推荐
- Springboot 和hutool文件上传下载
1.放开上传限制 servlet: multipart: enabled: true #默认支持文件上传 max-file-size: -1 #不做限制 max-request-size: -1 #不 ...
- vue初始化data数据
初始化data this.$data = this.$options.data() 初始化data中form this.$data.form = this.$options.data().form
- 解决Maven资源导出失败问题
由于 maven 的约定大于配置,maven 约定资源或配置文件放在 resources 目录下才能正常导出,但是如果我们将其放在 java 目录下,就需要在 pom.xml 添加如下配置,才能导出资 ...
- Windows10+VS2019从源码编译 Qt5
参考 Windows10+MSVC(VS2022)从源码编译QT5.12.11 - 知乎 (zhihu.com) qt-labs/vstools ~ qt-labs/vstools (github.c ...
- linux中用命令导出、导入mysql数据库表
一.导出数据 1.使用场景:在没有数据库可视化工具的情况下备份导出数据库. 命令如下: mysqldump -u用户名 -p 数据库名 > 数据库名.sql mysqldump -u root ...
- Scala集合排序
Scala集合排序有三种方法:sorted.sortBy().sortWith() (1)sorted 对一个集合进行自然排序,通过传递隐式的Ordering源码中有两点值得注意的地方:1.sorte ...
- thinkphp6+composer+无集成工具 配置php项目环境
安装composer 下载地址:https://getcomposer.org/Composer-Setup.exe 安装步骤 点击finish完成即可. 打开cmd输入composer查看是否安装成 ...
- jmeter压测dubbo接口,参数为dto时如何写传参及有错误时的分析思路
一.传参 1. 无论dubbo接口传参是否为dto,所有参数都是在args的tab传进去的. 2. 如果dto中有自定义对象,paramType为自定义dto名,paramValue为其他参数组成的j ...
- 新年快乐!体验Windows7黄金版,祝你2023财源广进!
新年快乐!体验Windows7黄金版,祝你2023财源广进! 首先,开机画面表示出它的土豪,并说明,它并不来自Microsoft. 然后: 它是2016年出现的. 改成64位的. 然后许可: 自定义: ...
- OS X Maven 安装与使用简介
Java真的很重很复杂,连项目构建和编译都得专门拉出来学,这里整理一下在OS X上使用Maven的注意事项. 一.安装 [bash] 1.从http://maven.apache.org/downlo ...