当前位置:网站首页>【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
边栏推荐
- Ansible playbook and ansible roles (III)
- How to develop programming learning with zero foundation during college
- talent showing itself! Oceanbase was selected into the 2021 "sci tech innovation China" open source innovation list
- Implementation of master-slave replication and master-master replication for MySQL and MariaDB databases
- How to perform disaster recovery and recovery for kubernetes cluster? (22)
- How to abstract a problem into a 0-1 knapsack problem in dynamic programming
- JVM foundation > G1 garbage collector
- [Jianzhi offer] Jianzhi offer 05 Replace spaces
- 【LeetCode】300.最长上升子序列
- [sword finger offer] sword finger offer 35 Replication of complex linked list
猜你喜欢

微信小程序提现功能

数据库每日一题---第10天:组合两个表

Ansible基础和常用模块(一)

Prefix sum and difference

Producer consumer model under multithreading model

JVM Basics - > What are the thread shared areas in the JVM

Yyds dry inventory insider news: Series high-frequency interview questions, worth a visit!

Ansible foundation and common modules (I)

Pat grade A - 1167 Cartesian tree (30 points) (buildtree + level traversal)

Redis optimization
随机推荐
【LeetCode】33. 搜索旋转排序数组
PE installation win10 system
孙老师版本JDBC(2022年6月12日21:34:25)
Implementation of master-slave replication and master-master replication for MySQL and MariaDB databases
Yyds dry goods inventory solution sword finger offer: the first non repeated character in the character stream
Audio and video technology development weekly 𞓜 234
Producer consumer model under multithreading model
打新债开户安全么,新手该怎么操作?
[probability theory and mathematical statistics] final review: formula summary and simple examples (end)
Configuring Dingding notification of SQL audit platform archery
NoSQL - redis configuration and optimization (II) high availability, persistence and performance management
Hostvars in ansible
Mysql case when then函数使用
JVM Basics - > how GC determines that an object can be recycled
Logstash timestamp converted to UNIX nanosecond nano second time
Mr. Sun's version of JDBC (21:34:25, June 12, 2022)
3.5 setup and teardown of test classes
Redis optimization
Ansible summary (VI)
C#读取word中表格数据