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 ...
随机推荐
- Account Manager privacy agreement
Account Manager privacy agreement [Account Manager] (hereinafter referred to as "we") )We ...
- 哲讯科技携手江苏大摩半导体借助SAP ERP打造数字化转型升级
项目启动会2022年10月28日,无锡哲讯科技携手江苏大摩半导体科技有限公司SAP ERP系统实施项目启动大会于江苏大摩的上海总部会议室胜利召开.双方项目组核心成员,共同见证了本次SAP ERP实施项 ...
- 【服务器数据恢复】Linux服务器分区不能挂载的数据恢复案例
服务器数据恢复环境:某品牌PowerEdge系列服务器,磁盘阵列存储型号为该品牌MD3200系列存储,分配lun:linux centos 7操作系统,EXT4文件系统. 服务器故障:服务器在工作中由 ...
- acl规则问题
在acl规则中网络地址与广播地址包含在规则范围内
- C语言学习记录(二)
C语言学习记录(二) 一.知识要点(C语言概述) 1.C语言的发展历史和特点 C语言的发展历史 C语言最早在B语言的基础上开发出来,并于1972年在一台计算机上首次实现. C语言最开始只是为描述和实现 ...
- IDEA EduTools Plugin Learning Cause
背景 编程培训需求,能够检测学生的输入内容与预期一致,有课程大纲 IDEA Plugin EduTools 是一个非常出色的培训工具,具备在IDE中学习,能够通过单元测试验证正确错误,能够设置用户输入 ...
- Ubuntu 18.04 安装基本应用
Ubuntu 谷歌浏览器下载:第一步打开终端,在终端输入下载命令wget https://dl.google.com/linux/direct/google-chrome-stable_current ...
- Web安全测试之XSS【转】
作者: 小坦克 来源: 博客园 原文链接:http://www.cnblogs.com/TankXiao/archive/2012/03/21/2337194.html XSS 全称(Cross ...
- mybatis-关联查询2-多对一关联查询
或者多表单独查询方式
- Demo of canvas, canvas optimization and svg
It used the canvas to draw the curves in the old project, and the client felt that it was vague, so ...