当前位置:网站首页>[牛客网刷题 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
边栏推荐
- Review of the losers in the postgraduate entrance examination
- SolidWorks工程图中添加中心线和中心符号线的办法
- Some test points about coupon test
- Programming features of ISP, IAP, ICP, JTAG and SWD
- Experience sharing of software designers preparing for exams
- 2022.7.5DAY597
- VS Code指定扩展安装位置
- AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
- [higherhrnet] higherhrnet detailed heat map regression code of higherhrnet
- Advanced function learning in ES6
猜你喜欢

Serial communication relay Modbus communication host computer debugging software tool project development case

php \n 换行无法输出

Fiddler break point

Programming features of ISP, IAP, ICP, JTAG and SWD

Google colab loads Google drive (Google drive is used in Google colab)
![[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)](/img/94/b4df1ce2861a851fcd8de3e08540b0.png)
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)

对存储过程进行加密和解密(SQL 2008/SQL 2012)

STM32基础知识—内存映射

P1223 排队接水/1319:【例6.1】排队接水

Pdf document signature Guide
随机推荐
【acwing】789. Range of numbers (binary basis)
Serial communication relay Modbus communication host computer debugging software tool project development case
SolidWorks工程图中添加中心线和中心符号线的办法
【acwing】786. 第k个数
.NET配置系统
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
fiddler-AutoResponder
Interface test
JMeter loop controller and CSV data file settings are used together
mysql插入数据创建触发器填充uuid字段值
The request object parses the request body and request header parameters
Inno setup packaging and signing Guide
Deadlock caused by non clustered index in SQL Server
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
Kotlin实现微信界面切换(Fragment练习)
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
[sword finger offer] 42 Stack push in and pop-up sequence
Can I open a stock trading account online? Is it safe
Postman interface test VI
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?