当前位置:网站首页>LeetCode 987. Vertical order transverse of a binary tree - Binary Tree Series Question 7
LeetCode 987. Vertical order transverse of a binary tree - Binary Tree Series Question 7
2022-07-03 01:28:00 【CP Coding】
Given the root
of a binary tree, calculate the vertical order traversal of the binary tree.
For each node at position (row, col)
, its left and right children will be at positions (row + 1, col - 1)
and (row + 1, col + 1)
respectively. The root of the tree is at (0, 0)
.
The vertical order traversal of a binary tree is a list of top-to-bottom orderings for each column index starting from the leftmost column and ending on the rightmost column. There may be multiple nodes in the same row and same column. In such a case, sort these nodes by their values.
Return the vertical order traversal of the binary tree.
Example 1:
Input: root = [3,9,20,null,null,15,7] Output: [[9],[3,15],[20],[7]] Explanation: Column -1: Only node 9 is in this column. Column 0: Nodes 3 and 15 are in this column in that order from top to bottom. Column 1: Only node 20 is in this column. Column 2: Only node 7 is in this column.
Example 2:
Input: root = [1,2,3,4,5,6,7] Output: [[4],[2],[1,5,6],[3],[7]] Explanation: Column -2: Only node 4 is in this column. Column -1: Only node 2 is in this column. Column 0: Nodes 1, 5, and 6 are in this column. 1 is at the top, so it comes first. 5 and 6 are at the same position (2, 0), so we order them by their value, 5 before 6. Column 1: Only node 3 is in this column. Column 2: Only node 7 is in this column.
Example 3:
Input: root = [1,2,3,4,6,5,7] Output: [[4],[2],[1,5,6],[3],[7]] Explanation: This case is the exact same as example 2, but with nodes 5 and 6 swapped. Note that the solution remains the same since 5 and 6 are in the same location and should be ordered by their values.
Constraints:
- The number of nodes in the tree is in the range
[1, 1000]
. 0 <= Node.val <= 1000
This question is related to 314. Binary Tree Vertical Order Traversal It's almost exactly the same , The change is that nodes with the same row number and column number need to be arranged from small to large according to the node value . Problem solving ideas are also in 314. Binary Tree Vertical Order Traversal Slightly changed on the basis of , When doing horizontal sequence traversal , Take one more line number , That is, the line number and node value of each node are represented by tuples tuple(row, val) Form into the column array . In this way, at the end, just sort each column , Because it is traversed in horizontal order, the line number is from small to large , Therefore, sorting will only change the order of node values with the same line number , Just meet the requirements .
class Solution:
def verticalTraversal(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
llist, rlist = [], []
lindex, rindex = 0, 1
q = deque([(root, 0, 0)])
while q:
n = len(q)
for i in range(n):
node, row, col = q.popleft()
if col <= 0:
if len(llist) > -col:
llist[-col].append((row, node.val))
else:
llist.append([(row, node.val)])
else:
if len(rlist) > col - 1:
rlist[col - 1].append((row, node.val))
else:
rlist.append([(row, node.val)])
if node.left:
q.append((node.left, row + 1, col - 1))
if node.right:
q.append((node.right, row + 1, col + 1))
llist.reverse()
wlist = llist + rlist
res = []
for arr in wlist:
arr.sort()
res.append([v[1] for v in arr])
return res
边栏推荐
- [day 29] given an integer, please find its factor number
- Androd gradle's substitution of its use module dependency
- Type expansion of non ts/js file modules
- [技术发展-23]:DSP在未来融合网络中的应用
- 2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
- 给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
- MySQL basics 03 introduction to MySQL types
- [interview question] 1369 when can't I use arrow function?
- MySQL foundation 04 MySQL architecture
- Cut point of undirected graph
猜你喜欢
Niu Ke swipes questions and clocks in
[untitled]
[技术发展-23]:DSP在未来融合网络中的应用
Soft exam information system project manager_ Real topic over the years_ Wrong question set in the second half of 2019_ Morning comprehensive knowledge question - Senior Information System Project Man
MySQL --- 数据库查询 - 基本查询
Basis of information entropy
【无标题】
[principles of multithreading and high concurrency: 2. Solutions to cache consistency]
leetcode 2097 — 合法重新排列数对
MySQL基础用法02
随机推荐
【FH-GFSK】FH-GFSK信号分析与盲解调研究
Force buckle 204 Count prime
MySQL foundation 07-dcl
Androd gradle's substitution of its use module dependency
Vim 9.0正式发布!新版脚本执行速度最高提升100倍
Button wizard play strange learning - go back to the city to buy medicine and add blood
Key wizard play strange learning - multithreaded background coordinate recognition
Uniapp component -uni notice bar notice bar
Database SQL language 01 where condition
Key wizard hit strange learning - automatic path finding back to hit strange points
[interview question] 1369 when can't I use arrow function?
Asynchronous, email and scheduled tasks
按键精灵打怪学习-自动回城路线的判断
Matlab finds the position of a row or column in the matrix
Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
数学知识:能被整除的数—容斥原理
tp6快速安装使用MongoDB实现增删改查
什么是调。调的故事
Mathematical knowledge: divisible number inclusion exclusion principle
The meaning of wildcard, patsubst and notdir in makefile