当前位置:网站首页>Sword finger offer 32: print binary tree from top to bottom
Sword finger offer 32: print binary tree from top to bottom
2022-06-11 05:46:00 【swindler.】
The finger of the sword Offer 32: Print binary tree from top to bottom
Breadth first traversal
class solution {
func levelOrder(_ root: TreeNode?) -> [[Int]] {
guard let rootVal = root else {
return [[Int]]()
var result = [[Int]](()// Two dimensional array returned from the result
var queue = [TreeNode]()// queue
queue.append(rootVal)
var curBlancecount = 1 // Record the remaining nodes to be printed in the current layer
var nextCount = 0 // Record the nodes to be recorded and printed on the next layer
var tempArray = [Int]() // One dimension of temporary two-dimensional array , Used to record each layer
while !queue.isEmpty{
let tempNode = queue.first
if let leftNode = tempNode?.left{
nextCount += 1
queue.append(leftNode)
}
if let rightNode = tempNode?.left{
nextCount += 1
queue.append(rightNode)
}
queue.removeFirst()
tempArray.append(tempNode!.val)
curBalancecount -= 1
if curBalancecount == 0{
result.append(tempArray)
tempArray.removefromAll()
curBalancecount = nextCount
nextCount = 0
}
}
return result
}
}
边栏推荐
- Project - Smart City
- 【深入kotlin】 - 初识 Flow
- Preliminary understanding of multi task learning
- MySQL string to array, merge result set, and convert to array
- 项目-智慧城市
- NDK learning notes (14) create an avi video player using avilib+window
- NDK learning notes (IX) POSIX sockect connection oriented communication
- 配置Rust编译环境
- Number of atoms (easy to understand)
- NDK learning notes (IV) functions, classes and exceptions of swig
猜你喜欢

NFC Development -- difference between ID card and IC card (M1 card and CPU card) (III)

Redis setup (sentinel mode)

NFC Development -- the method of using NFC mobile phones as access control cards (II)

NFC Development -- utility tools and development documents (IV)

Big meal count (time complexity) -- leetcode daily question

Cocoapods installation error

Customize the layout of view Foundation

Altiumdesigner2020 import 3D body SolidWorks 3D model

String sorting times --- bubble sorting deformation

Maximum number of points on the line ----- hash table solution
随机推荐
Elk log system practice (VI): comparison between vector and filebeat for technology selection
SQLite installation and configuration tutorial +navicat operation
NFC Development -- the method of using NFC mobile phones as access control cards (II)
Tencent X5 kernel initialization failed tbsreaderview not support by:***
In the future, how long will robots or AI have human creativity?
修复Yum依赖冲突
MinGW-W64安装说明
Multithreading tutorial (XXII) happens before principle
NDK learning notes (14) create an avi video player using avilib+window
Use com youth. banner. Solution to the inflateexception reported by the banner plug-in
智能门锁为什么会这么火,米家和智汀的智能门锁怎么样呢?
OJDBC在Linux系统下Connection速度慢解决方案
Games101 job 7-path tracing implementation process & detailed interpretation of code
Vscode plug-in development
NDK R21 compiles ffmpeg 4.2.2 (x86, x86_64, armv7, armv8)
Yoyov5's tricks | [trick8] image sampling strategy -- Sampling by the weight of each category of the dataset
[go deep into kotlin] - get to know flow for the first time
“All in ONE”一个平台解决所有需求,运维监控3.0时代已来
安装Oracle数据库
Handle double quotation mark escape in JSON string