LeetCode 103. Binary Tree Zigzag Level Order Traversal

UniApp X 网络请求避坑指南:从 JS 到 UTS 的 JSON 数据处理全解析 摘要:UniAppX开发中处理JSON数据时,UTS强类型特性要求开发者改变JS习惯。本文对比两种主流方案:1)UTSJSONObject通过keypath访问数据,兼容JS但缺乏类型提示;2)type+泛型方案,通过定义数据类型实现强类型校验和智能提示,推荐用于固定数据结构。文章详细演示了两种方式的具体实现、转换方法及实战案例,并提供了类型定义技巧和注意事项,帮助开发者编写更稳定高效的跨平台应用。 阅读详情

LeetCode 103. Binary Tree Zigzag Level Order Traversal

Solution1:基于层次遍历的微改

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int> > res;
        if (!root) return res;
        vector<int> temp;
        queue<TreeNode* > my_que; //队列用来暂时存储二叉树节点
        my_que.push(root);
        int cur = 1, next = 0, symbol = 0;
        while (!my_que.empty()) {
            temp.push_back(my_que.front()->val);
            cur--;
            if (my_que.front()->left) {
                my_que.push(my_que.front()->left);
                next++;
            }
            if (my_que.front()->right) {
                my_que.push(my_que.front()->right);
                next++;
            }
            my_que.pop();
            if (cur == 0) {
                if (symbol % 2 == 1) {
                    reverse(temp.begin(), temp.end());
                    res.push_back(temp);
                } else res.push_back(temp);
                temp.clear();
                cur = next;
                next = 0;
                symbol++;
            }
        }
        return res;
    }
};

Solution2:用两个栈来保存数据,时间复杂度为O(n)O(n)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int> > res;
        if(root == NULL)
            return res;
        vector<int> temp;
        stack<struct TreeNode* > stack_node[2];//栈数组,数组的每个元素均是栈,栈的数据类型是TreeNode*
        int cur = 0, next = 1;
        stack_node[cur].push(root);
        while(!stack_node[0].empty() || !stack_node[1].empty()) {
            temp.push_back(stack_node[cur].top()->val);
            if(cur == 0) {
                if(stack_node[cur].top()->left != NULL)
                    stack_node[next].push(stack_node[cur].top()->left);
                if(stack_node[cur].top()->right != NULL)
                    stack_node[next].push(stack_node[cur].top()->right);
            } else {
                if(stack_node[cur].top()->right != NULL)
                    stack_node[next].push(stack_node[cur].top()->right);
                if(stack_node[cur].top()->left != NULL)
                    stack_node[next].push(stack_node[cur].top()->left);
            }
            stack_node[cur].pop();
            if(stack_node[cur].empty()) {
                res.push_back(temp);
                temp.clear();
                cur = 1 - cur;
                next = 1 - next;
            }
        }
        return res;
    }
};
数据资产入表——解读100页数据资产入表解读与分析【附全文阅读】 本文档《数据资产入表:解读与分析》面向企业财务、数据管理、法务合规及高管等核心岗位,系统解析数据资产入表全流程。以"政策-实操-价值"为主线,首先解读入表政策要求,明确会计处理规范;其次详细拆解数据盘点、治理、确权、评估等实操步骤,提供行业案例参考;最后分析入表难点及后续运营策略,为企业提供从政策理解到落地执行的一站式解决方案。 阅读详情

相关推荐

Qt快速读取Excel文件

网上很多读取方式都是如下形式,太慢了,打开文件+读取文件要6s以上,实在是受不了! QAxObject excel("Excel.Application"); excel.setProperty("Visible", false); QAxObject *work_books = excel.querySubObject("WorkBooks"); work_boo...

c+c++的博客 2万+

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level ord

crazy_jack 9868

LLM - windows下的Dify离线部署:从镜像打包到无网环境部署(亲测,包含插件部署)

LLM - windows下的Dify离线部署:从镜像打包到无网环境部署(亲测,包含插件部署)

ocean00008899的博客 1285

LeetCode 103. Binary Tree Zigzag Level Order Traversal 题解

本题是二叉树层序遍历的变种,主要考察对 BFS 的灵活运用。使用标志位控制遍历方向可以在收集完一层后反转,也可以使用双端队列优化注意空树的处理通过本题可以深入理解 BFS 在二叉树遍历中的应用。

cannonjinx的博客 2991

Uncertainty-Aware Mean Teacher(UA-MT)

本文提出了一种新的基于不确定性的半监督学习框架(UA-MT),通过额外利用未标记的数据从3D MR图像中分割左心房。具体地说,本文建立了一个教师模型和一个学生模型,学生模型通过最小化标注数据上的分割监督损失和所有输入数据上的与教师模型预测输出的一致性损失进行优化。如果没有未标记输入中的注释,教师模型的预测目标可能不可靠且有噪声。:在估计不确定性 U 的指导下,我们过滤掉相对不可靠**(高不确定性),整个体积** U 的不确定性**是 {u} ∈ RH×W ×D。过滤掉高不确定性的,也就是熵大于某个值的。

Alexa_的博客 1869

搭建适用于lammps中opls-aa力场的文件流程

现在我已经解决了以上问题,并且通过我的方法可以实现从空白文档到搭建完成以上全部内容,后期只要细心检查即可。需要的data文件最关键是获得相应阴阳离子的mmol文件,因此问题转化为构建一份mmol文件。主要用到的软件及程序:gaussian09,makemol程序(tranal),opls-aa的参数文件。二面角个数:成角原子序号及对应的参数(这里PF6的结构没有二面角,所以为0)参考文献个数:文献名(空够行数即可,这些是给自己的备忘))和自身体系对应的参考文献,修改或填入对应的参数即可。

baguette的博客 2886

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 [...

LeeWei 223

glm-4v-9b环境配置全攻略:从CUDA版本冲突到bitsandbytes异常排查(RTX A4000实测)

本文详细介绍了在RTX A4000显卡上配置GLM-4V-9B多模态大模型的完整流程,重点解决CUDA版本冲突和bitsandbytes异常问题。通过4-bit量化技术显著降低显存占用,提供从环境预检到量化部署的实战指南,帮助开发者在有限显存环境下高效运行大模型。

weixin_30369041的博客 405

LeetCode—103. Binary Tree Zigzag Level Order Traversal

LeetCode—103. Binary Tree Zigzag Level Order Traversal 题目 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/ 之字形层序遍历二叉树。 思路及解法 因为昨天刚做过层序遍历二叉树的题102. Binary Tree Level ...

pnnngchg的博客 377

扣子(Coze)搭建智能体完整实战指南 —— 从零基础入门到写小说及其他需求扩展开发

随着大语言模型进入应用爆发期,我们正在经历从“寻找提示词”向“构建智能体”的范式转移。扣子(Coze)作为字节跳动推出的一站式AI智能体开发与运营平台,极大地降低了AI应用开发的门槛,让非技术人员也能快速搭建具备感知、决策、行动能力的智能体。 本文将手把手带你从零开始,在扣子平台上搭建一个功能完备的**小说创作智能体**,并基于此展示如何将同一套架构扩展到其他需求场景。无论你是写作爱好者、内容创作者,还是希望用AI赋能日常工作的职场人士,这套完整的技术方案都将帮助你快速上手。

huanghm88的专栏 636

三、参考比较法之城市建成区提取——提取

在正式开始提取之前,还是要继续啰嗦一下,其实基于夜间灯光数据建成区提取,可以分为好几个层次,基于县一级、市一级、省一级,很多论文的做法都是以市一级为准,也就是统计好市一级城市的每一年的统计年鉴中的建成区面积作为备用。有人可能会问为什么不以县一级,由于县一级的统计数据一般存在缺失,很难形成一个长时间序列的数据,所以一般采取的是市一级。前文也进行建议,如果确实要基于县一级进行提取可以参考前面理论介绍那篇文章中的相关建议。那么当我们确定阈值后如何将城市建成区提取出来?

端木宛白的博客家园 5401

[LeetCode] 103. Binary Tree Zigzag Level Order Traversal

原题链接:

Ber03的博客 273

华为od 面试手撕代码题【完成任务最少小时】

华为od 面试手撕代码题【完成任务最少小时】

MISAYAONE的博客 1574

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

哈哈的个人专栏 2775

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

LeetCode - 103. Binary Tree Zigzag Level Order Traversal

题目 LeetCode - 103. Binary Tree Zigzag Level Order Traversal 题目链接 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 参考博客 https://blog.csdn.net/whutshiliu/article/details/96142289 ...

virgilshi的博客 206

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

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

ziquanbang的专栏 248

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Leetcode 103 Binary Tree Zigzag Level Order Traversal

这是一道二叉树的Z字形遍历的问题。原本觉得不是很难,但中间磕磕绊绊遇到了许多小问题,费了我很大功夫才通过。首先和一般的BFS不同,Z字形遍历需要在一层遍历之后在下一层反向遍历。由于BFS常常用到队列这个数据结构,根据这个特点,我自然而然地想到了用栈来实现这种遍历。因为一层的遍历顺序正好和下一层是反的,先遍历到的结点在下一层是后遍历的。所以利用栈的“后进先出”的性质可以很方便地解决这个问题。由于奇偶...

tjumiao的博客 291

LeetCode103——Binary Tree Zigzag Level Order Traversal

题目链接 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: Given bina...

星辰的博客 209

[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

追梦船的专栏 794

[LeetCode 103] Binary Tree Zigzag Level Order Traversal

题目链接:binary-tree-zigzag-level-order-traversal 类似的题型: 1 Binary Tree Level Order Traversal 2 Binary Tree Level Order Traversal II 3 Minimum Depth of Binary Tree 4 Sum Root to Leaf Numbers

瓦尔登湖 690

LeetCode No.103 Binary Tree Zigzag Level Order Traversal

LeetCode No.103 Binary Tree Zigzag Level Order Traversal

黄健伟的博客 264

[leetcode] 103. Binary Tree Zigzag Level Order Traversal @ python

原题 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...

闲庭信步 554

【LeetCode】103. Binary Tree Zigzag Level Order Traversal

【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

网瘾少年康复中心 423

Leetcode 103. Binary Tree Zigzag Level Order Traversal 二叉树Zigzag遍历 解题报告

1 解题思想首先这道题和 Leetcode 102. Binary Tree Level Order Traversal 二叉树按层遍历 解题报告 很接近,Zigzag同样是按层遍历,但是区别是对于高度为奇数(从0开始计数的话,则是第1 3 5 7)的层,当层的遍历需要反过来。做法: 1、采用102题目中的方式,无论是DFS还是BFS都行 2、完成1后,对所有奇数层进行翻转这题的写法和102

MebiuW的专栏 1861

C++ Leetcode 103. Binary Tree Zigzag Level Order Traversal

103. Binary Tree Zigzag Level Order Traversal #1、题目大意 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 ...

chicwzh的博客 435

【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: Gi

nova0831的博客 259
上一篇: LeetCode 107. Binary Tree Level Order Traversal II
下一篇: LeetCode 100. Same Tree
Allenlzcoder
博客等级 码龄10年 925粉丝 512原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值