当前位置:网站首页>[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树
[牛客网刷题 Day5] JZ77 按之字形顺序打印二叉树
2022-07-07 08:14:00 【strawberry47】
题目:
给定一个二叉树,返回该二叉树的之字形层序遍历,(第一层从左向右,下一层从右向左,一直这样交替)
思路:
拿到题目的第一想法就是–使用队列呀,奇偶的时候存的东西不一样;行不通。。。
后来又想到使用双边队列,分情况从哪边进哪边出,发现找不到什么规律。。。
看了答案,发现用到了两个栈;也有使用队列,只不过每隔一层就反向打印一下
根据reverse的思路,我花了二十分钟写出来了代码:
class Solution:
def Print(self, pRoot: TreeNode):
# write code here
if pRoot is None:
return None
res = []
q = queue.Queue()
q.put(pRoot)
flag = 0
while not q.empty():
cur_res = []
n = q.qsize()
for i in range(n):
node = q.get()
cur_res.append(node.val)
if node.left:
q.put(node.left)
if node.right:
q.put(node.right)
if flag % 2 != 0:
cur_res.reverse()
res.append(cur_res)
flag = flag+1
return res
答案:
双栈(没咋看明白):
import copy
class Solution:
def Print(self , pRoot: TreeNode) -> List[List[int]]:
head = pRoot
res = []
if not head:
# 如果是空,则直接返回空list
return res
s1, s2 = [], []
# 放入第一次
s1.append(head)
while s1 or s2:
temp = []
# 遍历奇数层
while s1:
node = s1[-1]
# 记录奇数层
temp.append(node.val)
# 奇数层的子节点加入偶数层
if node.left:
s2.append(node.left)
if node.right:
s2.append(node.right)
s1.pop()
# 数组不为空才添加
if len(temp):
res.append(copy.deepcopy(temp))
# 清空本层数据
temp.clear()
# 遍历偶数层
while s2:
node = s2[-1]
# 记录偶数层
temp.append(node.val)
# 偶数层的子节点加入奇数层
if node.right:
s1.append(node.right)
if node.left:
s1.append(node.left)
s2.pop()
if len(temp):
res.append(temp)
return res
边栏推荐
- 【acwing】789. 数的范围(二分基础)
- Postman interface test II
- Es classes and objects, prototypes
- LeetCode 练习——113. 路径总和 II
- ArcGIS operation: converting DWG data to SHP data
- 一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
- @Configuration, use, principle and precautions of transmission:
- 2022.7.4DAY596
- 移动端通过设置rem使页面内容及字体大小自动调整
- 浅谈日志中的返回格式封装格式处理,异常处理
猜你喜欢

Fiddler break point

The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file

Adb 实用命令(网络包、日志、调优相关)

串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
![[sword finger offer] 42 Stack push in and pop-up sequence](/img/f4/eb69981163683c5b36f17992a87b3e.png)
[sword finger offer] 42 Stack push in and pop-up sequence

【二开】【JeecgBoot】修改分页参数

Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is

Smart city construction based on GIS 3D visualization technology

Appx代码签名指南

Deconvolution popular detailed analysis and nn Convtranspose2d important parameter interpretation
随机推荐
The method of word automatically generating directory
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
嵌入式工程师如何提高工作效率
Programming features of ISP, IAP, ICP, JTAG and SWD
The landing practice of ByteDance kitex in SEMA e-commerce scene
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
1321:【例6.3】删数问题(Noip1994)
IPv4 socket address structure
fiddler-AutoResponder
.NET配置系统
Postman interface test V
Parameter sniffing (2/2)
【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
HDU-2196 树形DP学习笔记
Learning records - high precision addition and multiplication
Methods of adding centerlines and centerlines in SolidWorks drawings
ISP、IAP、ICP、JTAG、SWD的编程特点
Es classes and objects, prototypes
Programming features of ISP, IAP, ICP, JTAG and SWD
Study summary of postgraduate entrance examination in November