当前位置:网站首页>Hierarchical traversal of binary tree
Hierarchical traversal of binary tree
2022-06-22 03:41:00 【Yu who wants to fly】
Give you a binary tree , Please return to its button Sequence traversal The resulting node value . ( That is, layer by layer , Access all nodes from left to right ).
Example :
Binary tree :[3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
Return its sequence traversal result :
[
[3],
[9,20],
[15,7]
]
This is a question on Li Kou , But I didn't do it , So I understand the official practice .
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<List<Integer>>();
if(root==null){
return ret;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
// Root in line
queue.offer(root);
while(!queue.isEmpty()){
List<Integer> level = new ArrayList<Integer>();
int currentLevelSize = queue.size();
// Every time the parent node leaves the queue , Add its child nodes to the queue
for(int i=1;i<=currentLevelSize;++i){
// Out of line operation , Get elements from the head of the team
TreeNode node = queue.poll();
level.add(node.val);
if(node.left!=null){
// The left child is not empty. The left child joins the team
queue.offer(node.left);
}
if(node.right!=null){
// The right child is not empty , The right kid joins the team
queue.offer(node.right);
}
}
ret.add(level);
}
return ret;
}
}
The main idea of the official practice is ,
- Root in line
- Traverse the current queue , Dequeue nodes
- When the node leaves the queue , Add its child nodes to the queue , Ensure that the queue is not empty after each traversal
- When the queue is empty , Represents that all traversals have been completed
边栏推荐
- 未來已來:雲原生時代
- 1299. replace each element with the largest element on the right
- MySQL 45 lecture learning notes (IV) index
- Shelling of ESP law of reverse crackme
- docker 安装redis
- The difference between quick failure and safe failure
- 3de 机器人吸盘抓box
- 3DE recover from design
- 魔法方法《六》__enter__和__exit__
- MySQL 45 lecture notes (I) execution of an SQL statement
猜你喜欢

Why is setinterval so easy to get stuck in the high and low level

Scheduling function: Splunk operator Controller Manager

Cloud native architecture (03) - Architecture

3000 yuan projector comparison and evaluation, dangbei D3x beats Jimi new Z6 x

How to break through the sales dilemma of clothing stores

分析Iceberg合并任务解决数据冲突

MySQL 45 lecture learning notes (II) execution of SQL update statements

3de 新建仿真状态

OAK相机如何实现同步?

美容院怎样做活动
随机推荐
剑指 Offer 68 - II. 二叉树的最近公共祖先
C51的一些日记
Talk about the Flink waterline
mysql-索引创建、优化分析、索引优化
所有项目的资源
H指数问题
replacement has 2 rows, data has 0, 解决R语言如何动态生成dataframe
Mysql 45讲学习笔记(一)一条sql语句的执行
Pan micro e-cology V9 information disclosure vulnerability
Rabbmitmq simple mode < 1 >
How to break through the sales dilemma of clothing stores
利用jemalloc解决flink的内存溢出问题
Cloud native architecture (02) - what is cloud native
The cloned VMware virtual host network card cannot be started solution
达梦数据库客户端屏蔽sql关键字
Implementation of synchronization and atomic operation by mutex mutex in golang concurrent programming
多线程 interrupt用法
Select in golang concurrent programming
Golang standard library time
Simple introduction to thoroughly understand anti shake and throttling