Leetcode: Binary Tree Zigzag Level Order Traversal

CCC数字钥匙3.0标准解读(22) 介绍设备制造商服务器和车辆制造商服务器之间的通信协议. 阅读详情

Question

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).

For example:
Given binary tree {3,9,20,#,#,15,7},
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
confused what “{1,#,2,3}” means? > read more on how binary tree is serialized on OJ.

Show Tags
Show Similar Problems


Analysis

This problem can be sees as an extension of the level order traversal. One flag variable is used to decide whether the current list will be reversed.


Solution

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def zigzagLevelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """

        if root==None:
            return []

        queue, lst, cur, res = [root], [], [], []
        lastnum, curnum = 1, 0
        flag = 0      # if false, print all nodes in current level in the reverse order

        while queue!=[]:
            while lastnum>0:
                temp = queue[0]
                queue = queue[1:]
                lastnum -= 1
                lst.append(temp.val)

                if temp.left!=None:
                    queue.append(temp.left)
                    curnum += 1
                if temp.right!=None:
                    queue.append(temp.right)
                    curnum += 1

            if flag%2==1:
                newlst = []
                for elem in lst:
                    newlst = [elem] + newlst
            else:
                newlst = lst

            res.append(newlst)

            lastnum = curnum
            curnum = 0
            lst = []
            flag += 1

        return res




Error Path

  1. forget to update lst to be [] after the inner while. keep an eye on it !
  2. when reversing lst, write newlst = newlst +[elem] instead of newlst = [elem] + newlst.

Take home message

In python, if flag=1, and flag = ~flag. The flag will not be change to be 1 as expected, but changed to be -1.

ODrive0.5.5源码分析(1) ADC的处理 odrive 0.5.5 ADC 阅读详情

相关推荐

高阶玩法——Dify生成的数据自动写入mysql数据库

采用自定义工具方式,利用 python编写一个服务sql_save_post,将接收到的内容写入指定数据库的表中,并为该服务撰写一个 OpenAPI 的 schema。配置工作流:我们以写入天气预报为例,在生成天气预报内容后,连接自定义工具“sql_save_post”,将所需的字段配置完成后,实现将生成的内容自动写入mysql数据库中。编写OpenAPI的schema:在dify平台的工具栏目,创建自定义工具,输入工具名称“sql_save_post”。工具:sql_save_post;

qq_44493378的博客 8549

LeetCode: Binary Tree Zigzag Level Order Traversal

Problem: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:

Tingmei的专栏 1251

ElementUI中的el-table解决宽度问题 - 根据内容自动撑开

在使用element-ui中,会发现表格组件el-table在未指定宽度情况下,会自动计算并给表格宽度赋值。但实际开发中,有时需要根据内容实际长度自动撑开显示,由内容的多少而决定表格的宽度,而不是默认宽度为100%。在默认情况下表格宽度为100%显示,并且列项未指定宽度情况下,剩余部分会平均分配宽度,本案例实际需求是由内容自动撑开显示。componentUpdated

觉醒法师的专栏 1万+

LeetCode 103:Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary

哈哈的个人专栏 2777

macOS 发票查验全攻略:从证书导入到Safari调试

本文详细介绍了在macOS上进行发票查验的全流程,包括证书下载、钥匙串访问导入、Safari浏览器调试等关键步骤。重点解决了验证码不显示等常见问题,提供了证书信任设置和开发者工具使用的实用技巧,帮助用户顺利完成税务发票在线查验。

weixin_29204749的博客 550

SAR成像处理基本知识2——图像尺寸与回波尺寸的关系

阅读本文前,请先了解上一篇文章(SAR成像处理基本知识1——常用专用名词解释) 1. SAR图像尺寸预估方法 以C波段2m@20km的SAR图像为例,假设天线中心下视角为35° ,说明图像尺寸的估计方法。 1.1 距离向尺寸估计 地距分辨率为2m时,距离向分辨率约为1.15m,则要求信号带宽不小于130MHz,假设信号带宽为150MHz,采样率为180MHz,那么距离向分辨单元为0.833m。 地距幅宽为20km时,距离向幅宽约为20*sind(3...

爱学习的小伟的博客 4959

[LeetCode] 015: Binary Tree Zigzag Level Order Traversal

[Problem]Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given

1.02^365的成长裂变 3624

AD快速上手笔记

本文详细介绍了使用Altium Designer从零创建PCB项目全流程:新建工程、绘制原理图(元件放置、连线、电源端口)、编译检查、生成PCB、布局布线(含铺铜)、DRC检查及输出Gerber与钻孔文件。重点强调了库选择、参数修改、层切换、铺铜设置和生产文件导出规范,确保设计无误,便于顺利下单生产。

qq_53529450的博客 50

leetcode笔记:Binary Tree Zigzag Level Order Traversal

与题目Binary Tree Level Order Traversal 相似,可以使用BFS,同时用一个bool记录是从左到右还是从右到左读取数据,每一层遍历结束就翻转一下。

liyuefeilong的专栏 2003

hwasan实例分析踩内存之七:CamX进程chifeature2base怪异的Cause: use-after-free

摘要:本文分析了高通camx进程中的指针异常crash问题,通过构建hwasan版本确认存在内存踩踏现象。日志显示为"use-after-free"类型错误,具体为线程T175672在已释放内存区域执行写入操作。堆栈分析表明ChiFeature2Base::PipelineDestroyCb()函数访问了已被ChiFeature2Base::DestroyFeatureData()释放的pPipelineData对象。该问题源于多线程环境下内存管理不同步,导致释放后仍被引用。解决方案通过

1091

FanucROBOGUIDE软件二次开发:使用ROBOGUIDEAPI进行编程

菜单栏:位于界面顶部,提供文件、编辑、视图、插入、格式、工具、窗口和帮助等选项。工具栏:包含常用的工具按钮,如新建、打开、保存、运行、停止、机器人控制等。工作站视图:显示工作站的3D模型,用户可以在这里放置机器人、工件、工具和其他设备。机器人控制面板:用于控制机器人运动,包括手动移动、程序编辑和运行。程序编辑器:在这里可以编写和编辑机器人程序,支持Fanuc的R-30iB Plus和R-30iB控制柜的程序格式。状态栏:显示当前工作站的状态信息,如机器人位置、程序状态等。

kkchenjj的博客 1423

LeetCode :: Binary Tree Zigzag Level Order Traversal [tree, BFS]

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary

Eleven的专栏 992

QT Android开发之Android端usb调试模式设置与问题解决

QT Android开发之Android端usb调试模式设置与问题解决 一.QT android开发android端usb调试模式设置 QT android开发环境搭建完成后,android设备需要打开usb调试模式才能正常连接,下面以小米ipad为例进行设置(其他手机和ipad设置方法类似) 二.小米ipad打开usb调试模式 1.设置----我的设备-----全部参数信息-----OS版本---点击这里6次以上就进入开发者模式。

热土程序园,利他愉己~ 1143

LeetCode 103: Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order TraversalGiven a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate b

一只码畜 277

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LeetCode题解:Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).For example: Given binary tree

追风筝的吃货,汪~ 859

InTheHand.Net.Personal+BlueTooth4,蓝牙开发

项目中有蓝牙开发的必备dll,现在网上相关的文档博客不少,有的说的多有的说少,但是却很少有这两个dll,分享一下。

[C++]LeetCode: 101 Binary Tree Zigzag Level Order Traversal

题目: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given

cindy_niu的专栏 1098

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

Leetcode 103: Binary Tree Zigzag Level Order Traversal

Question: Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given b...

兔子窝 179

leetCode(23):Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary

佛曰不可说 728

leetcode question 103 : Binary Tree Zigzag Level Order Traversal

问题:Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:Given binary tre...

csdn609387481的博客 174

Leetcode 103:Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree...

qq_34314736的博客 139

Leetcode:Binary Tree Zigzag Level Order Traversal

Description: Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Giv...

weixin_30902251的博客 67

Baozi Leetcode solution 103: Binary Tree Zigzag Level Order Traversal

Problem Statement Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). ...

weixin_30832143的博客 740

LeetCode: 103. Binary Tree Zigzag Level Order Traversal

题目 Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary t...

liveway6的博客 142

LeetCode[Tree]: Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tr

chfe910的专栏 655
上一篇: Leetcode: Binary Tree Right Side View
下一篇: Leetcode: Count Complete Tree Nodes
ayst123
博客等级 码龄16年 16粉丝 275原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值