当前位置:网站首页>【LeetCode】103. Zigzag sequence traversal of binary tree
【LeetCode】103. Zigzag sequence traversal of binary tree
2022-06-12 22:22:00 【LawsonAbs】
1. subject
2. thought
(1) Level traversal , You only need to output , Just adjust it ok. If you adjust it as you enter it , Will lead to changes in the whole sequence , Cause uncontrollable
(2) data structure queue Use .
For common data structures and functions , We should keep it in mind . For example, flip a list, have access to reversed Method , But it's not flipping in place .
3. Code
# 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() # Define a queue
cnt = 0
tmp = [root]
while(len(tmp)):
for i in range(len(tmp)):# take temp Put the elements in one by one into que in
que.put(tmp[i])
tmp.clear()
cur_res = [] # Node value of current layer
while(que.qsize()): # Judge que The content in
cur = que.get() # Get the header element
# 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) # It's very strange why such an error is reported ?
if cnt % 2==1:
cur_res= list(reversed(cur_res))
res.append(copy.deepcopy(cur_res))
cnt+=1
return res
边栏推荐
- Audio and video technology development weekly 𞓜 234
- [proteus simulation] simple digital tube timer clock
- Es6+ new content
- Yyds dry goods inventory solution Huawei machine test: weighing weight
- flutter系列之:flutter中常用的GridView layout详解
- JVM Basics - > What are the thread shared areas in the JVM
- 四元数简介
- NoSQL - redis configuration and optimization (II) high availability, persistence and performance management
- LNMP platform docking redis service
- 项目里面的traceID的设计
猜你喜欢

Database daily question --- day 10: combine two tables
![[image denoising] image denoising based on trilateral filter with matlab code](/img/f2/770a0e2938728e731c18c0a66f7c12.png)
[image denoising] image denoising based on trilateral filter with matlab code

Dolphin-2.0.3 cluster deployment document

设计消息队列存储消息数据的 MySQL 表格

JVM foundation - > three ⾊ mark

Audio and video technology development weekly 𞓜 234

Kotlin collaboration process - flow

Flutter series part: detailed explanation of GridView layout commonly used in flutter

Ansible summary (VI)

Yyds dry inventory insider news: Series high-frequency interview questions, worth a visit!
随机推荐
Is it safe to open an account in tonghuashun? How to open an account
[simple] 155 Minimum stack
【LeetCode】53.最大子数组和
A 42 year old senior executive of a large factory reminds people aged 30-39 that these six habits that make you stronger should be developed as soon as possible
大学期间零基础如何开展编程学习
Qt Quick 3D学习:鼠标拾取物体
JVM foundation > G1 garbage collector
Create a virtual thread using loom - David
Modstartcms modular station building system v3.3.0 component function upgrade, event triggering enhancement
Audio and video technology development weekly 𞓜 234
【LeetCode】5. 最长回文子串
Have you really learned the common ancestor problem recently?
JVM foundation - > What garbage collectors does the JVM have?
"Oracle database parallel execution" technical white paper reading notes
MySQL introduction and installation (I)
JVM foundation > CMS garbage collector
One article to quickly understand whether there are security risks in your network
Su embedded training day13 - file IO
Research Report on market supply and demand and strategy of tizanidine industry in China
[probability theory and mathematical statistics] final review: formula summary and simple examples (end)