当前位置:网站首页>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边栏推荐
- C#应用程序界面开发基础——窗体控制(1)——Form窗体
- Mathematical Knowledge: Steps - Nim Games - Game Theory
- Is there a handling charge for spot gold investment
- tp6快速安装使用MongoDB实现增删改查
- Kivy教程大全之如何在 Kivy 中创建下拉列表
- 【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
- Key wizard play strange learning - front desk and Intranet send background verification code
- 测试右移:线上质量监控 ELK 实战
- [day 29] given an integer, please find its factor number
- d,ldc构建共享库
猜你喜欢

Leetcode 2097 - Legal rearrangement of pairs

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

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

力扣 204. 计数质数

Matlab Doppler effect produces vibration signal and processing

【C语言】指针与数组笔试题详解
![[C language] detailed explanation of pointer and array written test questions](/img/24/c2c372b5c435cbd6eb83ac34b68034.png)
[C language] detailed explanation of pointer and array written test questions

C application interface development foundation - form control (2) - MDI form

MySQL basic usage 02

攻克哈希的基本概念与实现
随机推荐
给你一个可能存在 重复 元素值的数组 numbers ,它原来是一个升序排列的数组,并按上述情形进行了一次旋转。请返回旋转数组的最小元素。【剑指Offer】
The industrial scope of industrial Internet is large enough. The era of consumer Internet is only a limited existence in the Internet industry
Basic concept and implementation of overcoming hash
Matlab Doppler effect produces vibration signal and processing
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
Test shift right: Elk practice of online quality monitoring
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
[self management] time, energy and habit management
Canvas drawing -- bingdd
按键精灵打怪学习-自动寻路回打怪点
MySQL --- 数据库查询 - 条件查询
The latest analysis of tool fitter (technician) in 2022 and the test questions and analysis of tool fitter (technician)
Excel removes the data after the decimal point and rounds the number
C#应用程序界面开发基础——窗体控制(2)——MDI窗体
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
[技术发展-23]:DSP在未来融合网络中的应用
音程的知识的总结
d. LDC build shared library
Draw love with go+ to express love to her beloved
Type expansion of non ts/js file modules