当前位置:网站首页>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边栏推荐
- 测试右移:线上质量监控 ELK 实战
- 对非ts/js文件模块进行类型扩充
- Force buckle 204 Count prime
- MySQL foundation 07-dcl
- MySQL基础用法02
- Androd gradle's substitution of its use module dependency
- Leetcode 6103 - minimum fraction to delete an edge from the tree
- Canvas drawing -- bingdd
- Basic remote connection tool xshell
- Kivy教程大全之如何在 Kivy 中创建下拉列表
猜你喜欢

How is the mask effect achieved in the LPL ban/pick selection stage?

Matlab Doppler effect produces vibration signal and processing

强化学习 Q-learning 实例详解

软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053

Androd Gradle 对其使用模块依赖的替换

Arduino dy-sv17f automatic voice broadcast
![[fh-gfsk] fh-gfsk signal analysis and blind demodulation research](/img/8a/8ca80f51a03341c982d52980c54b01.png)
[fh-gfsk] fh-gfsk signal analysis and blind demodulation research

MySQL

给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】

How wide does the dual inline for bread board need?
随机推荐
Arduino dy-sv17f automatic voice broadcast
leetcode 2097 — 合法重新排列数对
【QT】自定义控件的封装
MySQL
Key wizard hit strange learning - automatic path finding back to hit strange points
[interview question] 1369 when can't I use arrow function?
d,ldc構建共享庫
MySQL --- 数据库查询 - 基本查询
Kivy tutorial how to create drop-down lists in Kivy
Dotconnect for PostgreSQL data provider
Mathematical knowledge: divisible number inclusion exclusion principle
数学知识:台阶-Nim游戏—博弈论
Excel if formula determines whether the two columns are the same
d. LDC build shared library
Using tensorboard to visualize the model, data and training process
ThinkPHP+Redis实现简单抽奖
leetcode刷题_两数之和 II - 输入有序数组
【第29天】给定一个整数,请你求出它的因子数
[C language] detailed explanation of pointer and array written test questions
【FPGA教程案例6】基于vivado核的双口RAM设计与实现