Leetcode-103: Binary Tree Zigzag Level Order Traversal

倍福Twincat 3.0软件与C++通讯问题(ADS通讯) 倍福Twincat 3.0软件与C++通讯问题 倍福公司所开发的Twincat 3.0软件是基于Visual studio开发环境的一款自动化软件,目前其与C++程序通讯的方式主要还是靠ADS-DLL接口进行数据和信息传递,作者在Visual C++上进行混合编程开发,通过TwinCAT路由器和C语言API函数实现混合编程。 倍福软件提供: TcAdsDll.dll—动态链接库; TcAdsDll... 阅读详情

解法1:就是把102的按层次打印的那道题的程序拿过来,加上一个符号(fromRight)表示是否zigzag,该符号每行flip一次,然后每次zigzag的时候把对应的vector反过来。
该解法比较慢,因为要翻转。Leetcode的执行时间为6ms。

vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
    vector<vector<int> > vv;
    queue<TreeNode*> q;
    q.push(root);
    if (root) q.push(NULL);
    vector<int> v;
    bool fromRight=true;
    while(!q.empty()) {
        TreeNode* t=q.front();
        q.pop();
        if (t) {
            v.push_back(t->val);
                if (t->right) q.push(t->right);
                if (t->left) q.push(t->left);
        }
        else{  //line end reached
            if (v.size()) {
                if (fromRight) {
                    reverse(v.begin(), v.end());
                }
                vv.push_back(v);

            }
            if (q.empty())  break;
            v.clear();
            q.push(NULL);
            fromRight=!fromRight;
        }
   }
    return vv;
}

解法2:用deque。也是加上一个zigzag符号,每行flip。该解法跟102的解法2类似,但是用的deque,并且隔行分别用的pop_\front()/push_back() 和
pop_\back()/push_front()。该解法比较快,leetcode执行时间为4ms。

#include <iostream>
#include <queue>
#include <vector>
#include <algorithm>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
    vector<vector<int> > vv;
    deque<TreeNode*> dq;
    if (root) dq.push_back(root);
    vector<int> v;
    bool right2left=false;
    while(!dq.empty()) {
        TreeNode* t;
        int len=dq.size();
        while (len--) {
            if (right2left) {
                t=dq.front();
                dq.pop_front();
                v.push_back(t->val);
                if (t->right) dq.push_back(t->right);
                if (t->left) dq.push_back(t->left);
            }else{
                t=dq.back();
                dq.pop_back();
                v.push_back(t->val);
                if (t->left) dq.push_front(t->left);
                if (t->right) dq.push_front(t->right);
            }
        }
        vv.push_back(v);
        if (dq.empty())  return vv;
        v.clear();
        right2left=!right2left;
    }
    return vv;
}

int main()
{
//   vector<vector<int> > vv=zigzagLevelOrder(NULL);   
    TreeNode t1(3);
    TreeNode t2(9);
    TreeNode t3(20);
    TreeNode t4(15);
    TreeNode t5(7);

    t1.left=&t2;
    t1.right=&t3;
    t3.left=&t4;
    t3.right=&t5;

    vector<vector<int> > vv=zigzagLevelOrder(&t1);

    cout<<"["<<endl;
    for (auto v : vv) {
        cout<<"    [ ";
        for (auto i : v) {
            cout<<i<<" ";
        }
        cout<<"]"<<endl;
    }
    cout<<"]"<<endl;
    return 0;
}
Carla自动驾驶仿真一:快速安装与运行Carla 网上有不少的Windows版本的Carla的安装教程,这里简单记录下自己安装过程,写的也比较详细,基本不用看其他文章。 阅读详情

相关推荐

Windows下ArduPilot SITL仿真环境搭建与QGC地面站联调全攻略

软件在环仿真(SITL)是无人机开发中的关键一环,它通过将飞控固件运行在PC上,用数学模型模拟传感器数据,实现无硬件依赖的飞行逻辑验证。这项技术不仅降低了试错成本,更为地面站开发、自动化测试和任务规划提供了安全可控的试验场。在工程实践中,MAVLink协议作为飞控与地面站之间的通信桥梁,贯穿仿真、遥测与指令控制全程。结合QGroundControl(QGC)这一主流地面站,开发者可以直观地观察飞行状态、规划航点并记录日志。本文以Windows原生环境为例,详细介绍ArduPilot SITL的搭建流程、MA

weixin_31858171的博客 324

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

从ISCXTor2016到CICIDS2017:手把手教你下载和使用UNB的网络安全数据集

本文详细介绍了UNB大学发布的ISCXTor2016和CICIDS2017等网络安全数据集的下载与使用方法,特别适合加密流量识别和网络行为分析研究。文章提供了多种高效获取路径、文件结构解析及实战分析示例,帮助研究者快速上手这些真实场景捕获的精细标注数据集。

weixin_42524945的博客 644

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

【芯片驱动】2. CMT2300A配合硬件测试(灵敏度和发射功率)的软件实现

前言 在开发一款无线射频产品的时候,软件是一部分,硬件也是一部分。而决定无线收发性能的,首先是硬件的匹配电路,然后才到软件部分的优化。 一款无线射频产品,首先需要先决定是在那个频率范围内(当然是国家允许的范围内),然后硬件则需要在基于这个频点范围进行硬件的参数匹配,具体又分为发送功率匹配、天线匹配、和接收灵敏度的匹配。 现在我们使用2300a这款无线芯片来说,除非自己的团队是射频专家,则可以自己进行无线外围电路的设计外,另外一种更加快捷的方式,就是寻找原厂技术支持,他们会给...

sishuihuahua的博客 7065

revit二次开发环境搭建(2024版)(小白友好)

revit二次开发环境配置,SDK、addin manager和lookup新手友好安装教程

m0_56961251的博客 1425

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

安装测试豆包多模态纯视觉驱动-UI-TARS基于GUI的自动化智能体模型---AI大模型应用探索0010

字节跳动开源的UI-TARS项目基于Apache 2.0协议可商用,但最新桌面版v0.3.0移除了exe安装包。建议下载旧版v0.2.4的exe安装包进行安装,下载地址为:https://github.com/bytedance/UI-TARS-desktop/releases。注意要区分主项目UI-TARS和桌面版UI-TARS-desktop两个不同仓库。

添柴程序猿的专栏 693

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的博客 173

SMPLify-x-master | 复现记录

由于毕业设计中将会用到smpl模型,而smplify方法的各环境依赖版本比较老,复现不太容易,因此便进行了smplifyx方法使用smplx的方法。由于在过程中遇到较多坑,因此进行记录和分享。

qq_44916003的博客 5112

国社科基金申报书“选题与说明依据”用AI怎么写?借助Gemini 3 Pro一次理清研究演进逻辑

研究进展部分要明确指出已有研究的不足与局限,比如方法不足、数据或材料受限、研究视角单一等等局限,之后要找准研究切入点,自然引申出自己的研究问题。为提升课题申报书的质量,学术史梳理要遵循述评结合、中外结合、新老结合的原则,不仅“述”,更要“评”,兼顾国内研究与国际学术动态,统筹经典研究与最新成果。它是对选题的整体概述,需阐明研究的具体问题、视角以及核心概念。2)存在问题或争议:结合已有研究或实践现状,指出该领域在理论建构、测量工具、实践应用或研究结论方面仍存在的不足、空白或争议,明确研究的必要性;

智能AI让学术科研更容易 950

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

线性自抗扰控制(LADRC)系统算法框图

NLADRC自抗扰控制从Simulink仿真到PLC控制实现_自抗扰控制器 simulink仿真-CSDN博客本文介绍了如何将ADRC自抗扰控制算法从Simulink仿真应用到PLC控制中,包括Simulink框图、TD微分器模型、MATLAB函数的使用、SCL控制代码,以及非线性状态误差反馈控制率的实现。提供了详细的MATLAB仿真步骤和PLC源代码链接。ADRC自抗扰控制算法(含梯形图完整源代码和算法公式)_adrc算法-CSDN博客。

RXXW_Dor的博客 1472

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

vb 6.0并口开发操作实例

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

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

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

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

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

Leo_Chen008的博客 264

leetcode[103]:Binary Tree Zigzag Level Order Traversal

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

hahubaba的博客 409

LeetCode: Binary Tree Zigzag Level Order Traversal [103]

【题目】 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 /

Harry Huang 1013

C++详解Leetcode:103. Binary Tree Zigzag Level Order Traversal

原题思路与102题层次遍历最大的区别就是,此题要求先在开始层从左到右遍历,下一层再从右到左,再从左到右。。。依次类推,所以只要在层次遍历的代码中,加上一个计数变量level记录当前层,初始值为1,当下一个数是下一层时,判断level%2是否等于0,如果level为奇数则从左到右,如果为偶数,则将存储在vector中的这一层数进行逆转,然后存储到最后的结果向量中。codeclass Solution

楼上小宇___home 803

python写算法题:leetcode: 103. Binary Tree Zigzag Level Order Traversal

class Solution(object): def traversal(self, node, ret, level): if node==None: return if len(ret)<=level: ret.append([]) if level&1==1: ret[...

181

leetcode: 103. Binary Tree Zigzag Level Order Traversal

Difficulty Medium. 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). Fo...

JNing 266

leetcode 103: Binary Tree Zigzag Level Order Traversal

/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ clas

mikefan1991的博客 192
上一篇: Leetcode-104: Maximum Depth of Binary Tree
下一篇: LintCode 93: Balanced Binary Tree (Leetcode-110)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值