当前位置:网站首页>【LeetCode】103. 二叉树的锯齿形层序遍历
【LeetCode】103. 二叉树的锯齿形层序遍历
2022-06-12 22:17:00 【LawsonAbs】
1.题目
2.思想
(1)层次遍历,只需要在输出的时候,调整一下就ok。 如果在输入的时候就调整,会引来整个序列的变化,导致不可控
(2)数据结构queue的使用。
对于常见的数据结构和函数,我们要牢记于心。比如翻转一个list,可以使用 reversed 方法,但是它并非是就地翻转。
3.代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
import copy
from queue import Queue
class Solution:
def zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:
res = []
if root is None:
return res
que = Queue() # 定义一个队列
cnt = 0
tmp = [root]
while(len(tmp)):
for i in range(len(tmp)):# 将temp中的元素逐个放入到que中
que.put(tmp[i])
tmp.clear()
cur_res = [] # 当前这层的节点值
while(que.qsize()): # 判断que中的内容
cur = que.get() # 获取头元素
# print(cur.val,hight)
if cur.left is not None:
tmp.append(cur.left)
if cur.right is not None:
tmp.append(cur.right)
cur_res.append(cur.val) # 很奇怪为啥这么会报错?
if cnt % 2==1:
cur_res= list(reversed(cur_res))
res.append(copy.deepcopy(cur_res))
cnt+=1
return res
边栏推荐
- The kotlin coroutine -- coroutine context and exception propagation
- 设计消息队列存储消息数据的 MySQL 表格
- What are thread scheduler and timeslicing?
- JVM foundation > GC generation: minorgc majorgc fullgc mixed GC
- Xingda easy control ModbusRTU to modbustcp gateway
- Create a virtual thread using loom - David
- JVM Basics - > What are the JVM parameters?
- 【QNX Hypervisor 2.2 用戶手册】4.2 支持的構建環境
- Ansible-大总结(六)
- IPhone: save Boolean into core data - iphone: save Boolean into core data
猜你喜欢

Producer consumer model under multithreading model

数据库每日一题---第10天:组合两个表

SQL tuning guide notes 15:controlling the use of optimizer statistics

"Oracle database parallel execution" technical white paper reading notes

RAID disk array

PE安装win10系统

Have you really learned the common ancestor problem recently?

flutter系列之:flutter中常用的GridView layout详解

Preliminary use of jvisualvm

The programmer dedicated to promoting VIM has left. Father of vim: I will dedicate version 9.0 to him
随机推荐
Role of volatile keyword
Oracle livelabs experiment: introduction to Oracle Spatial
How to abstract a problem into a 0-1 knapsack problem in dynamic programming
Ansible roles project case (IV)
February 27th
SQL tuning guide notes 16:managing historical optimizer statistics
2021 rust survey results released: 9354 questionnaires collected
"Oracle database parallel execution" technical white paper reading notes
Use group_ Dplyr issues when using group_ by(multiple variables)
Ansible playbook和Ansible Roles(三)
Economist focuses on WTO MC12: digital economy may become an important issue
What is the difference between volatile variables and atomic variables?
June training (day 11) - matrix
经济学人聚焦WTO MC12:数字经济或成重要议题
How to ensure thread safety?
Yyds dry goods inventory solution sword finger offer: the first non repeated character in the character stream
SQL tuning guide notes 10:optimizer statistics concepts
Interpretation of OCP function of oceanbase Community Edition
Permission to query execution plan in Oracle Database
RAID disk array