当前位置:网站首页>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边栏推荐
- 数学知识:能被整除的数—容斥原理
- Machine learning terminology
- Niu Ke swipes questions and clocks in
- C#应用程序界面开发基础——窗体控制(3)——文件类控件
- Matlab Doppler effect produces vibration signal and processing
- 【无标题】
- Thinkphp+redis realizes simple lottery
- Mathematical knowledge: step Nim game game game theory
- MySQL foundation 06 DDL
- 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 basic usage 02

【C语言】指针与数组笔试题详解

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

力扣 204. 计数质数

Esp32 simple speed message test of ros2 (limit frequency)

Daily topic: movement of haystack

wirehark数据分析与取证A.pacapng
![leetcode:701. Insertion in binary search tree [BST insertion]](/img/bc/1dda73198488eb81b49be2c1dff6c2.png)
leetcode:701. Insertion in binary search tree [BST insertion]

【面试题】1369- 什么时候不能使用箭头函数?
![[untitled]](/img/fd/f6b90536f10325a6fdeb68dc49c72d.png)
[untitled]
随机推荐
Key wizard play strange learning - front desk and Intranet send background verification code
MySQL
MySQL --- 数据库查询 - 条件查询
How is the mask effect achieved in the LPL ban/pick selection stage?
tail -f 、tail -F、tailf的区别
[FPGA tutorial case 5] ROM design and Implementation Based on vivado core
Tp6 fast installation uses mongodb to add, delete, modify and check
【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
Basic concept and implementation of overcoming hash
基本远程连接工具Xshell
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
Key wizard hit strange learning - automatic path finding back to hit strange points
Now that the teenager has returned, the world's fireworks are the most soothing and ordinary people return to work~
leetcode 6103 — 从树中删除边的最小分数
[flutter] icons component (fluttericon Download Icon | customize SVG icon to generate TTF font file | use the downloaded TTF icon file)
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
[shutter] animation animation (the core class of shutter animation | animation | curvedanimation | animationcontroller | tween)
Asynchronous, email and scheduled tasks
Concise analysis of redis source code 11 - Main IO threads and redis 6.0 multi IO threads
[day 29] given an integer, please find its factor number