当前位置:网站首页>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

边栏推荐
- It is suggested that Siyuan notes can be compatible with third-party sync disks
- Yunna | how to manage the fixed assets of the company? How to manage the company's fixed assets better?
- [pyqt5 installation and use]
- [software test] MQ abnormal test encountered in disk-to-disk work
- Argocd user management, RBAC control, script login, APP synchronization
- Is it risky and safe to open a mobile stock account?
- 正则表达式常用语法解析
- 利用js自动解析执行xss
- OpenCV计算外包矩形
- LZ77 file compression
猜你喜欢

Aruba learning notes 05 configuration architecture WLAN configuration architecture

【 langage c】 devinez jeux numériques + applet d'arrêt

LeetCode-227-基本计算器||

AI acceleration gesture recognition experience based on efr32mg24

LZ77 file compression

R language practical application case: drawing part (III) - drawing of multiple combination patterns
![[WinForm] desktop program implementation scheme for screenshot recognition and calculation](/img/9f/e67af4386cff9febf0fb5203da03f0.jpg)
[WinForm] desktop program implementation scheme for screenshot recognition and calculation

Yunna | how to manage the fixed assets of the company? How to manage the company's fixed assets better?

JS texture style pie chart plug-in

Authing 支持 Zadig 啦!云原生用户统一认证快速对接
随机推荐
工作小记:一次抓包
10 years of software testing engineer experience, very confused
Chapitre 2 requête de base et tri
Canvas from getting started to persuading friends to give up (graphic version)
[applet automation minium] III. element positioning - use of wxss selector
Looking for peak [Abstract dichotomy exercise]
LZ77文件压缩
【数组&&字符串&&宏练习题】
【FLink】FLink Hash collision on user-specified ID “opt“. Most likely cause is a non-unique ID
Chapter 2 basic query and sorting
Quanzhi f1c100s/f1c200s learning notes (13) -- lvgl transplantation
Is it risky and safe to open a mobile stock account?
LZ77 file compression
关于flex布局justify-content:space-around最后一个不对齐的解决方法和为什么这样子解决是讨论
Which is a good fixed asset management system? What are the fixed asset management platforms?
Regular verification of ID number
cmake笔记
Program design of dot matrix Chinese character display of basic 51 single chip microcomputer
Zhongwang CAD professional 2022 software installation package download and installation tutorial
[download attached] several scripts commonly used in penetration testing that are worth collecting