当前位置:网站首页>[daiy5] jz77 print binary tree in zigzag order
[daiy5] jz77 print binary tree in zigzag order
2022-07-07 10:29:00 【strawberry47】
subject :
Given a binary tree , Returns the zigzag of the binary tree ,( The first floor is from left to right , The next floor is from right to left , All the time )
Ideas :
The first idea to get the title is – Use queue , Things stored in odd and even are different ; It won't work ...
Later, I thought of using bilateral queues , According to the situation, which side goes in and out , Find no rules ...
Looking at the answer , It is found that two stacks are used ; There are also queues , Just print it backwards every other layer
according to reverse The idea of , It took me twenty minutes to write the code :
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
answer :
Double stack ( I didn't see clearly ):
import copy
class Solution:
def Print(self , pRoot: TreeNode) -> List[List[int]]:
head = pRoot
res = []
if not head:
# If it's empty , Then return to null directly list
return res
s1, s2 = [], []
# Put it in for the first time
s1.append(head)
while s1 or s2:
temp = []
# Traverse odd layers
while s1:
node = s1[-1]
# Record odd layers
temp.append(node.val)
# Children of odd layers join even layers
if node.left:
s2.append(node.left)
if node.right:
s2.append(node.right)
s1.pop()
# Only add when the array is not empty
if len(temp):
res.append(copy.deepcopy(temp))
# Clear the data of this layer
temp.clear()
# Traverse even layers
while s2:
node = s2[-1]
# Record even layers
temp.append(node.val)
# Children of even layers join odd layers
if node.right:
s1.append(node.right)
if node.left:
s1.append(node.left)
s2.pop()
if len(temp):
res.append(temp)
return res
边栏推荐
猜你喜欢
Embedded background - chip
AHB bus in stm32_ Apb2 bus_ Apb1 bus what are these
IIC基本知识
fiddler-AutoResponder
MySQL insert data create trigger fill UUID field value
【acwing】789. Range of numbers (binary basis)
0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)
小程序跳转H5,配置业务域名经验教程
IO模型复习
P1031 [NOIP2002 提高组] 均分纸牌
随机推荐
【STM32】STM32烧录程序后SWD无法识别器件的问题解决方法
基于gis三维可视化技术的智慧城市建设
Smart city construction based on GIS 3D visualization technology
MySQL insert data create trigger fill UUID field value
Easyexcel read write simple to use
Learning records - high precision addition and multiplication
table宽度比tbody宽度大4px
关于hzero-resource报错(groovy.lang.MissingPropertyException: No such property: weight for class)
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
XML configuration file parsing and modeling
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
【acwing】789. Range of numbers (binary basis)
[detailed explanation of Huawei machine test] tall and short people queue up
Appx代码签名指南
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
Some properties of leetcode139 Yang Hui triangle
小程序跳转H5,配置业务域名经验教程
1324:【例6.6】整数区间
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例