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 ...
随机推荐
- vue 分页显示 引用Element框架
VUE组件 父页面-子组件-传值 单表查询图片上传: 注释为简单方法 图片显示方法: 红框处应该为地址+存图片的文件夹名 +当前图片的值 在vue中引用Element需要的步骤 1.在终端中输入网址 ...
- 1.Python3.5+Pyqt5+PyCharm+Opencv3.3+Qtdesigner开发环境配置
一:Python3.3和Pyqt5的安装 注意:两个的版本一定要对应,一定要对应,一定要对应,重要的事情说三遍. 因为我自己的电脑是64位的,所以我下载的都是64位版本的,且都是3.5版本的:这两个一 ...
- 使用std::string的结构不能使用memset
使用了std::string作为成员变量的结构体,千万不能使用memset进行初始化,否则程序会爆
- TP开发项目时遇到的问题记录
1.下载功能. TP自带Http下载类,使用时new一个就行,示例代码: public function download(){ //接收公文id $id = I('get.fid'); //根据公文 ...
- 记录安装perl-Verilog过程
开始,编译带Verilog::Netlist的脚本,报 YumRepo Error: All mirror URLs are not using ftp, http[s] or file.centos ...
- LightOJ - 1162 Min Max Roads
LightOJ - 1162 Min Max Roads 题解:在线倍增LCA和模拟ST表 让我们求从\(u->v\)最短路径上的边权最大值和边权最小值,那么我们可以利用倍增思想,类似其\(fa ...
- 配dns
cat /etc/resolv.conf
- Py_excel
py_excel xlrd 读excel workbook = xlrd.open_workbook(file_path) sheet = workbook.sheet_by_name(sheet_n ...
- Netbeans 16 的学习日志(购物车GUI)(建设中)
1.前期准备 Netbeans点我下载 Netbeans快捷键 学习视频1 郑老师的java 购物车实例 ①Netbeans 16没有中文,更老的版本可能会有,但就我目前使用来看,有中文反倒是一件坏事 ...
- spring boot2 jpa分页查询百万级数据内存泄漏
分页查询百万级数据,查询处理过程中发现内存一直飙升,最终处理程序会挂掉,通过jvisualvm可以发现频繁ygc 和fgc ,另外通过 jmap -histo:live ${pid} 命令可以看到jp ...