当前位置:网站首页>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边栏推荐
- Exness: Mercedes Benz's profits exceed expectations, and it is predicted that there will be a supply chain shortage in 2022
- Global and Chinese markets of nasal oxygen tubes 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese market of commercial cheese crushers 2022-2028: Research Report on technology, participants, trends, market size and share
- Grabbing and sorting out external articles -- status bar [4]
- 插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
- [width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)
- [Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice
- leetcode3、实现 strStr()
- Leetcode3, implémenter strstr ()
- leetcode-2. Palindrome judgment
猜你喜欢
![[solution] add multiple directories in different parts of the same word document](/img/22/32e43493ed3b0b42e35ceb9ab5b597.jpg)
[solution] add multiple directories in different parts of the same word document

Formatting occurs twice when vs code is saved

Audio and video engineer YUV and RGB detailed explanation

PHP campus movie website system for computer graduation design

Minecraft 1.18.1、1.18.2模组开发 22.狙击枪(Sniper Rifle)

Minecraft 1.18.1, 1.18.2 module development 22 Sniper rifle

Concept of storage engine
![抓包整理外篇——————状态栏[ 四]](/img/1e/2d44f36339ac796618cd571aca5556.png)
抓包整理外篇——————状态栏[ 四]

SQL statement

Adapter-a technology of adaptive pre training continuous learning
随机推荐
selenium 元素定位(2)
Adapter-a technology of adaptive pre training continuous learning
同一个 SqlSession 中执行两条一模一样的SQL语句查询得到的 total 数量不一样
Install redis
Flowable source code comments (36) process instance migration status job processor, BPMN history cleanup job processor, external worker task completion job processor
Grabbing and sorting out external articles -- status bar [4]
Genius storage uses documents, a browser caching tool
It's wrong to install PHP zbarcode extension. I don't know if any God can help me solve it. 7.3 for PHP environment
500 lines of code to understand the principle of mecached cache client driver
Virtual machine network, networking settings, interconnection with host computer, network configuration
Audio and video engineer YUV and RGB detailed explanation
Compact lidar global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
Text editing VIM operation, file upload
FTP server, ssh server (super brief)
Global and Chinese market of commercial cheese crushers 2022-2028: Research Report on technology, participants, trends, market size and share
[robot hand eye calibration] eye in hand
729. My schedule I / offer II 106 Bipartite graph
Flutter Doctor:Xcode 安装不完整
PHP campus movie website system for computer graduation design
阿裏測開面試題