当前位置:网站首页>[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
边栏推荐
- fiddler-AutoResponder
- P1031 [NOIP2002 提高组] 均分纸牌
- STM32 product introduction
- 关于hzero-resource报错(groovy.lang.MissingPropertyException: No such property: weight for class)
- Jump to the mobile terminal page or PC terminal page according to the device information
- 对存储过程进行加密和解密(SQL 2008/SQL 2012)
- Guid primary key
- Inno setup packaging and signing Guide
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- 使用U2-Net深层网络实现——证件照生成程序
猜你喜欢

0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)

Postman interface test II

LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件

【acwing】789. Range of numbers (binary basis)

PDF文档签名指南

Inno Setup 打包及签名指南

基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS

IO模型复习

Several schemes of building hardware communication technology of Internet of things

【acwing】786. 第k个数
随机推荐
根据设备信息进行页面跳转至移动端页面或者PC端页面
学习记录——高精度加法和乘法
Factorial implementation of large integer classes
SQLyog数据库怎么取消自动保存更改
XML configuration file parsing and modeling
IO模型复习
搭建物联网硬件通信技术几种方案
Talking about the return format in the log, encapsulation format handling, exception handling
EasyExcel读取写入简单使用
Postman interface test VI
Study summary of postgraduate entrance examination in July
Chris Lattner, père de llvm: Pourquoi reconstruire le logiciel d'infrastructure ai
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet
P1223 排队接水/1319:【例6.1】排队接水
STM32 Basics - memory mapping
Word自动生成目录的方法
Several schemes of building hardware communication technology of Internet of things
Differences between MCU and MPU
Review of the losers in the postgraduate entrance examination
Guid primary key