当前位置:网站首页>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边栏推荐
- Is there anything in common between spot gold and spot silver
- leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
- Telephone network problems
- Dotconnect for PostgreSQL data provider
- Button wizard play strange learning - automatic return to the city route judgment
- After reading this article, I will teach you to play with the penetration test target vulnhub - drivetingblues-9
- [fh-gfsk] fh-gfsk signal analysis and blind demodulation research
- Vim 9.0正式发布!新版脚本执行速度最高提升100倍
- [shutter] animation animation (the core class of shutter animation | animation | curvedanimation | animationcontroller | tween)
- 数学知识:Nim游戏—博弈论
猜你喜欢
随机推荐
Excel if formula determines whether the two columns are the same
Matlab Doppler effect produces vibration signal and processing
How wide does the dual inline for bread board need?
d,ldc構建共享庫
ThinkPHP+Redis实现简单抽奖
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
MySQL foundation 07-dcl
Excel calculates the difference between time and date and converts it into minutes
Excel removes the data after the decimal point and rounds the number
Dotconnect for PostgreSQL data provider
Mathematical knowledge: Nim game game theory
【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
Daily topic: movement of haystack
Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
[shutter] animation animation (shutter animation type | the core class of shutter animation)
Basic concept and implementation of overcoming hash
[my advanced journey of OpenGL learning] collation of Euler angle, rotation order, rotation matrix, quaternion and other knowledge
Arduino dy-sv17f automatic voice broadcast
Kivy教程大全之 创建您的第一个kivy程序 hello word(教程含源码)








