当前位置:网站首页>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
}
}
边栏推荐
- Informatica:数据质量管理六步法
- 27. Remove elements
- NDK learning notes (II)
- Fix Yum dependency conflict
- 数据接入平台方案实现(游族网络)
- handler
- Swap numbers (no temporary variables)
- If the MAC fails to connect with MySQL, it will start and report an error
- JS promise, async, await simple notes
- NDK learning notes (IX) POSIX sockect connection oriented communication
猜你喜欢

使用Genymotion Scrapy控制手机

Cocoapods installation error

Games101 job 7-path tracing implementation process & detailed interpretation of code

22. Generate parentheses

NDK learning notes (VI) Basics: memory management, standard file i/o

Maximum number of points on the line ----- hash table solution

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

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

BERT知识蒸馏

Stone game -- leetcode practice
随机推荐
初步了解多任务学习
Wxparse parsing iframe playing video
Multithreading tutorial (XXIII) thread safety without lock
Recursively process data accumulation
Multi threading tutorial (XXIV) cas+volatile
Getbackgroundaudiomanager controls music playback (dynamic binding of class name)
數組部分方法
Control your phone with genymotion scratch
MySQL circulates multiple values foreach, XML writing method
Maximum number of points on the line ----- hash table solution
安装Oracle数据库
After adding the header layout to the recyclerview, use the adapter Notifyitemchanged (POS,'test') invalid local refresh
Metabase源码二次开发之Clojure 安装
数据接入平台方案实现(游族网络)
ELK日志系统实战(六):技术选型之vector与filebeat对比
SQLite installation and configuration tutorial +navicat operation
Array partial method
Slide the receleview horizontally to the far right to listen to the page loading function
NDK learning notes (V)
Analyzing while experimenting - memory leakage caused by non static inner classes