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 ...
随机推荐
- 2、Maven
- vue3介绍-vue3创建项目-setup函数-ref和reactive-计算属性和监听-生命周期-toRefs-script setup的作用和lang=ts-vue后台管理模板
目录 vue3介绍-vue3创建项目-setup函数-ref和reactive-计算属性和监听-生命周期-toRefs-script setup的作用和lang=ts-vue后台管理模板 昨日内容回顾 ...
- 【转载】synopsys中工具介绍,VCS,DC,PT等
https://blog.csdn.net/fangxiangeng/article/details/80981536
- 403 forbidden 与 413Too Large
http://www.ccschy.com/shuma/12846.html https://blog.51cto.com/u_15127556/4543159 查的有关资料如下,最后的原因是服务器网 ...
- linux下进程通信总结
信号: 信号是通知发生了某种事件的机制,内核和进程都可能会向进程发送各种信号,进程也可以向自己发送信号.系统定义了一组标准信号类型,每种信号都拥有唯一的数值和用途.典型的信号递送是异步的,意味着进程可 ...
- 2.4G收发一体芯片NRF24L01P跟国产软硬件兼容 SI24R1对比
超低功耗高性能 2.4GHz GFSK 无线收发器芯片Si24R1Si24R1 是一颗工作在 2.4GHz ISM 频段,专为低功耗无线场合设计,集成嵌入式ARQ 基带协议引擎的无线收发器芯片.工作频 ...
- MyBatis(Plus) 打印SQL, 分析执行时间
MyBatis/MyBatis Plus打印的SQL调试起来比较麻烦 当然IDEA/eclipse都有类似mybatis log plugin这种插件来解析, 但是安装插件有些许弊端, 就写了个工具类 ...
- python40
Python Number 数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间. 以下实例在变量赋值时 Number 对象将被创建: v ...
- Spring Boot注册Servlet、Filter、Listener原理
如何使用 在Spring Boot中注册Servlet.Filter办法主要有3种,下面来看下具体例子,例子都采用Filter,Servlet同理. 第一种,使用FilterRegistrationB ...
- UVM reg model 常见问题记录
1.仿真log中报出大量的"include_coverage not located, did you mean ***"? (1) user在构建register model或者 ...