当前位置:网站首页>LeetCode 103. Binary tree zigzag level order transverse - Binary Tree Series Question 5
LeetCode 103. Binary tree zigzag level order transverse - Binary Tree Series Question 5
2022-07-06 02:16:00 【CP Coding】
Given the root
of a binary tree, return the zigzag level order traversal of its nodes' values. (i.e., from left to right, then right to left for the next level and alternate between).
Example 1:
Input: root = [3,9,20,null,null,15,7] Output: [[3],[20,9],[15,7]]
Example 2:
Input: root = [1] Output: [[1]]
Example 3:
Input: root = [] Output: []
Constraints:
- The number of nodes in the tree is in the range
[0, 2000]
. -100 <= Node.val <= 100
The title requires level Z Traverse the whole binary tree in font order , That is, in behavioral units , A line from left to right , The next line is from right to left , In this way, the binary tree is traversed alternately .
If you are familiar with the horizontal traversal binary tree algorithm , This question is relatively simple . I think about it , utilize BFS The algorithm traverses in behavioral units . Define a queue queue, Put the whole row of nodes into the queue each time , In each cycle, all nodes in the current queue ( That is, the nodes of the whole line ) Take out one by one , Put the values into a temporary array , Then take out the left and right child nodes of the node ( If it exists ) Put it in the queue . As required Z Font type , Every other row needs to reverse the temporary array . Finally, add all the numbers of the temporary array to the answer array .
class Solution:
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
res = []
l2r = True
q = deque([root])
while q:
n = len(q)
level = []
for i in range(n):
node = q.popleft()
level.append(node.val)
if node.left:
q.append(node.left)
if node.right:
q.append(node.right)
if l2r:
l2r = False
else:
l2r = True
level.reverse()
res.append(level)
return res
边栏推荐
- [solution] add multiple directories in different parts of the same word document
- Jisuanke - t2063_ Missile interception
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- 竞价推广流程
- Get the relevant information of ID card through PHP, get the zodiac, get the constellation, get the age, and get the gender
- Install redis
- leetcode-2. Palindrome judgment
- Blue Bridge Cup embedded_ STM32_ New project file_ Explain in detail
- Visualstudio2019 compilation configuration lastools-v2.0.0 under win10 system
- 通过PHP 获取身份证相关信息 获取生肖,获取星座,获取年龄,获取性别
猜你喜欢
2022 edition illustrated network pdf
Pangolin Library: subgraph
Virtual machine network, networking settings, interconnection with host computer, network configuration
插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
MySQL lethal serial question 1 -- are you familiar with MySQL transactions?
Jisuanke - t2063_ Missile interception
MySQL index
Adapter-a technology of adaptive pre training continuous learning
Lecture 4 of Data Engineering Series: sample engineering of data centric AI
【MySQL 15】Could not increase number of max_open_files to more than 10000 (request: 65535)
随机推荐
[community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
在线怎么生成富文本
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
Adapter-a technology of adaptive pre training continuous learning
Method of changing object properties
[Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice
Use the list component to realize the drop-down list and address list
selenium 等待方式
Flowable source code comments (36) process instance migration status job processor, BPMN history cleanup job processor, external worker task completion job processor
Use image components to slide through photo albums and mobile phone photo album pages
FTP server, ssh server (super brief)
[robot hand eye calibration] eye in hand
MySQL index
How does redis implement multiple zones?
Overview of spark RDD
【社区人物志】专访马龙伟:轮子不好用,那就自己造!
怎么检查GBase 8c数据库中的锁信息?
Competition question 2022-6-26
1. Introduction to basic functions of power query