当前位置:网站首页>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边栏推荐
- Blue Bridge Cup embedded_ STM32 learning_ Key_ Explain in detail
- 使用npm发布自己开发的工具包笔记
- Bidding promotion process
- 【coppeliasim】6自由度路径规划
- Redis list
- 【coppeliasim】高效传送带
- [solution] every time idea starts, it will build project
- RDD conversion operator of spark
- 一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
- MySQL learning notes - subquery exercise
猜你喜欢

2022 edition illustrated network pdf

Using SA token to solve websocket handshake authentication

leetcode-两数之和

Campus second-hand transaction based on wechat applet

Leetcode3. Implement strstr()

Audio and video engineer YUV and RGB detailed explanation

Exness: Mercedes Benz's profits exceed expectations, and it is predicted that there will be a supply chain shortage in 2022

Selenium waiting mode

Minecraft 1.16.5 生化8 模组 2.0版本 故事书+更多枪械

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
随机推荐
通过PHP 获取身份证相关信息 获取生肖,获取星座,获取年龄,获取性别
selenium 等待方式
01. Go language introduction
Xshell 7 Student Edition
2022年PMP项目管理考试敏捷知识点(8)
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
leetcode-两数之和
数据工程系列精讲(第四讲): Data-centric AI 之样本工程
A basic lintcode MySQL database problem
Comments on flowable source code (XXXV) timer activation process definition processor, process instance migration job processor
论文笔记: 极限多标签学习 GalaXC (暂存, 还没学完)
selenium 元素定位(2)
Regular expressions: examples (1)
Overview of spark RDD
Concept of storage engine
Genius storage uses documents, a browser caching tool
02. Go language development environment configuration
Global and Chinese markets of nasal oxygen tubes 2022-2028: Research Report on technology, participants, trends, market size and share
Flutter Doctor:Xcode 安装不完整
Publish your own toolkit notes using NPM