当前位置:网站首页>21 - vertical traversal of binary tree
21 - vertical traversal of binary tree
2022-07-23 14:49:00 【JH_ Cao】
1. subject
GitHub Brush the algorithm together
Title Description

Ideas
/**
Ideas :
- From the top down BFS
- From left to right DFS
*/
/**
Code explanation :
0. Define a dictionary , Store the results of each column [key: Value] = [Int: [Int]]
- Define a queue , Deposit node
- Define a queue , Storage location , On the left -1, On the right +1
- Take out the queue every time , Update dictionary's key - Value
- Traversing the dictionary value, Traverse from left to right , Put in res
*/

- Code
func verticalOrder(_ root: TreeNode?) -> [[Int]] {
var res = [[Int]]()
if root == nil { return res }
var dict: [Int: [Int]] = [Int: [Int]]()
var queueValue = [TreeNode]()
if let root = root {
queueValue.append(root)
}
var queuePosition = [Int]()
queuePosition.append(0)
var leftPos = Int.max
while !queueValue.isEmpty {
let node = queueValue.removeFirst()
let pos = queuePosition.removeFirst()
var subArr = dict[pos, default: [Int]()]
subArr.append(node.val)
dict[pos] = subArr
if node.left != nil {
queueValue.append(node.left!)
queuePosition.append(pos - 1)
}
if node.right != nil {
queueValue.append(node.right!)
queuePosition.append(pos + 1)
}
leftPos = min(leftPos, pos)
}
for i in leftPos..<leftPos + dict.count {
if let subArr = dict[i] {
res.append(subArr)
}
}
return res
}
- Submit results

边栏推荐
- 利用shell脚本实现封禁扫描频率过高的ip
- Sword finger offer19 regular expression
- CPU, memory, disk speed comparison
- 【测试平台开发】23. 接口断言功能-保存接口断言和编辑回显
- mysql函数汇总之数学函数
- Official wechat product! Applet automation framework minium sharing Preview
- 直播课堂系统03-model类及实体
- Sword finger offer 46. translate numbers into strings
- C language implementation of classroom random roll call system
- MySQL unique index has no duplicate value, and the error is repeated
猜你喜欢

Several hole filling methods of point cloud (mesh) (1)

Cmake notes

Detailed tutorial of typora drawing bed configuration

MySQL unique index has no duplicate value, and the error is repeated

Opencv calculation outsourcing rectangle

104 maximum depth of binary tree and 543 diameter of binary tree and 124 maximum path sum of binary tree

Towhee weekly model

mysql函数汇总之字符串函数

String function of MySQL function summary

运维高级作业03
随机推荐
Transferred from Yuxi information disclosure: products such as mRNA covid-19 vaccine and Jiuzhou horse tetanus immunoglobulin are expected to be on the market within this year.
Towhee weekly model
4. Find the median of two positive arrays
【测试平台开发】21. 完成发送接口请求显示响应头信息
[array & String & Macro exercise]
Chapter 2 basic query and sorting
What is per title encoding?
Detailed tutorial of typora drawing bed configuration
Some libraries that can perform 2D or 3D triangulation
C language introduction practice (11): enter a group of positive integers and sum the numbers in reverse order
基金开户网上办理是否安全?谁给解答一下
mysql唯一索引无重复值报错重复
直播课堂系统02-搭建项目环境
对象使用过程中背后调用了哪些方法
Work notes: one time bag grabbing
Chapitre 2 requête de base et tri
右键新建txt,新建文本文件不见了,通过添加注册表就可以解决,找来找去办法解决不了的终极办法
[applet automation minium] III. element positioning - use of wxss selector
【我可以做你的第一个项目吗?】GZIP的细节简介和模拟实现
Sword finger offer19 regular expression