AcWing 38. 二叉树的镜像

题解:AcWing 38 二叉树的镜像 等知名刷题平台精心挑选而来,并结合各平台提供的算法标签和难度等级进行了系统分类。题目涵盖了从基础到进阶的多种算法和数据结构,旨在为不同阶段的编程学习者提供一条清晰、平稳的学习提升路径。输入一个二叉树,将它变换为它的镜像。对于二叉树的镜像,其实就是交换它的左右子树。第一行包含整数N,表示二叉树的节点数。第二行给出二叉树的顺序遍历。递归遍历原树的所有节点,将每个节点的左右儿子互换即可。《AcWing 38 二叉树镜像》 #二叉树#输出镜像二叉树的先序遍历。本文分享的必刷题目是从。 阅读详情

具体代码如下:

/**
 * 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:
    void mirror(TreeNode* root) {
        if(!root) return;
        swap(root -> left, root -> right);
        mirror(root -> left);
        mirror(root -> right);
    }
};

 

skill使用教程,从小白到大神进阶 (2)突然无法使用(NodeJS安装参考):如果正常使用中,突然发现claude命令无法启动claude-code了,可能是claude-code正在后台自动更新,等待几分钟后再尝试使用,如果还是不行,请先运行下列代码移除claude-code,注意,如果你的node安装目录不同,要替换成自己的安装目录:rm -rf /opt/homebrew/lib/node_modules/@anthropic-ai/claude-code。安装完成后,让我们启动 Claude Code,看看它长什么样。 阅读详情

相关推荐

初识机会网络仿真器ONE中ExternalMovement移动模型(导入外部移动数据)

最近半个月一直在研究ONE中关于ExternalMovement移动模型的源代码,在网上几乎没有找到相关的资料,很是苦恼,如果有共同研究方向的小伙伴欢迎联系我,共同讨论进步。 下面记录一下几点感悟: 1、使用外部数据的前提是在ONE的配置文件中增加及修改相关代码: Group.movementModel = ExternalMovement ExternalMovement.file = move...

qq_36974792的博客 659

AcWing--38. 二叉树的镜像

38. 二叉树的镜像 题目: 输入一个二叉树,将它变换为它的镜像。 思路: 前序遍历+递归 遍历每个节点,如果有子节点,就交换两个子节点,并向下递归,当交换了所有非叶子节点后,就得到了二叉树的镜像。 样例 输入树: 8 / \ 6 10 / \ / \ 5 7 9 11 [8,6,10,5,7,9,11,null,null,null,null,null,null,null,null] 输出树: 8 / \ 10 6 /

weixin_45000199的博客 149

STM32实战:如何用GPS信号同时同步海康摄像头和Livox Mid-360雷达(附完整接线图)

本文详细介绍了如何利用STM32实现GPS信号分频,同步海康摄像头和Livox Mid-360雷达的时间同步方案。通过硬件架构设计、STM32信号处理核心实现及设备接口规范,解决了多传感器数据融合中的时间对齐难题,适用于自动驾驶和机器人导航领域。

weixin_29182481的博客 596

【ACWing】38. 二叉树的镜像

题目地址: https://www.acwing.com/problem/content/37/ 输入一个二叉树,将它变换为它的镜像。 数据范围: 树中节点数量[0,100][0,100][0,100]。 代码如下: struct TreeNode { int val; TreeNode *left, *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: void

数学、算法爱好者的博客 971

【芯片验证随心记】1.SystemVerilog中,rand和randc的区别?如何用constraint实现randc?

在SystemVerilog里,randc 的“不重复循环”特性是用一个 周期性排列器(permutation engine) 实现的,而这个引擎在 LRM 里并没有公开。我们只能在 “用户空间” 用 rand + constraint 去模仿它的行为。每次随机化只取 queue.pop_front(),用完再洗。在4次随机化内,y会依次出现0,1,2,3各一次(顺序随机)每次随机化,x可能是0-3中的任意值,可能连续多次相同。每次随机化时,在未使用的值里随机挑一个;第5次随机化时,重新生成新的排列周期。

qq_43172512的博客 342

Kylin系统离线环境下的dpkg依赖包高效部署指南

本文详细介绍了在Kylin系统离线环境下高效部署dpkg依赖包的完整方案,包括预下载依赖包、构建本地APT仓库以及高级故障排查技巧。通过实战经验和优化建议,帮助用户解决依赖地狱问题,提升部署效率,特别适用于银河麒麟V10等内网环境。

weixin_29051245的博客 386

AcWing 38 二叉树的镜像

题目描述: 输入一个二叉树,将它变换为它的镜像。 样例 输入树: 8 / \ 6 10 / \ / \ 5 7 9 11 [8,6,10,5,7,9,11,null,null,null,null,null,null,null,null] 输出树: 8 / \ 10 6 / \ / \ 11 9 7 ...

昂昂累世士的博客 165

SAP WM模块事务码(TCODE)大全

OL07 Data transfer stor.bins LDK34->RLPLA 仓储位LDK34 ->RLPLA

matinal 當冬夜漸暖 。公众号:matinal 5846

AcWing 38. 二叉树的镜像

题目描述 输入一个二叉树,将它变换为它的镜像。 样例 输入树: 8 / \ 6 10 / \ / \ 5 7 9 11 [8,6,10,5,7,9,11,null,null,null,null,null,null,null,null] 输出树: 8 / \ 10 6 / \ / \ 11 9 7 ...

yj_coder的博客 145

【系统分析师】2025年上半年真题:案例分析-答案及详解(回忆版)

摘要: 试题一考查面向对象分析,涉及用例模型构建的四个阶段(识别参与者、合并需求、细化描述、调整模型),分析CRM系统的参与者与用例,并说明用例间的包含、扩展和泛化关系。 试题二探讨嵌入式Docker,包括容器的轻量性、可移植性及隔离性含义,10项措施与7个规范方面的对应关系,以及优化容器切换时间的三种方法(如无状态连接优化)。 试题三聚焦数据库设计,分析订单表的函数依赖与范式分解(BCNF),讨论分解后是否保持函数依赖及无损性,并针对触发器的性能缺陷提出优化方案。 试题四解析Web开发中的MVC架构与SS

数据知道的博客 6605

自适应控制 (Adaptive Control) 算法详解及案例分析

自适应控制是一种能够在线调整控制器参数以适应系统动态变化的控制方法。其核心思想是通过实时估计系统参数或调整控制器参数,使系统在不确定性或动态变化的环境中仍能保持稳定性和性能。

闲人编程的博客 4085

剑指 offer acwing 38 二叉树的镜像

题面 题面 我们观察可以发现:所有子节点都交换了位置,所以我们只需要从根节点开始递归,然后交换根节点的左右儿子即可 代码 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * };

qq_44791484的博客 135

【ROS2】机械臂MoveIt2

navigation2 专注于移动机器人的导航与定位,处理大范围空间的底盘运动;MoveIt2 专注于机械臂的操作规划,处理多关节协同的末端执行器运动。在复合机器人(如,移动机械臂)navigation2 负责移动到目标区域,MoveIt2 负责机械臂抓取。官网:https://moveit.picknik.ai/main/index.html。

郭老二 597

剑指Offer打卡day18——AcWing 38. 二叉树的镜像

【题目描述】 AcWing 38. 二叉树的镜像 【思路】 可以发现每一个子树的左右节点都是交换的。遍历二叉树,交换左右节点(整个节点都交换)。 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class S

JohnnyLin 213

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

OV9734 DATASHEET,完整规格书

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

ACWing 38.二叉树镜像(python版)

题目描述 输入一个二叉树,将它变换为它的镜像。 样例: 思路 观察后可发现,镜像后的二叉树就是把所有节点的左右儿子交换。具体实现步骤如下: 1、用递归的方法遍历二叉树的节点,非叶子节点交换该节点的左右儿子 2、遇到叶子节点则返回 代码实现 # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left

fuzzy__H的博客 266

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

ACWING38. 二叉树的镜像(剑指offer)

输入一个二叉树,将它变换为它的镜像。 样例 输入树: 8 / 6 10 / \ / 5 7 9 11 [8,6,10,5,7,9,11,null,null,null,null,null,null,null,null] 输出树: 8 / 10 6 / \ / 11 9 7 5 [8,10,6,11,9,7,5,null,null,null,null,null,null,null,nu...

tomjobs的博客 310

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

AcWing 38. 二叉树的镜像(C++)- 二叉树

题目链接:https://www.acwing.com/problem/content/37/ 题目如下: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class

qq_40467670的博客 129

【ACWing】39. 对称的二叉树

题目地址: https://www.acwing.com/problem/content/38/ 请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。 数据范围: 树中节点数量[0,100][0,100][0,100]。 代码如下: struct TreeNode { int val; TreeNode *left, *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; clas

数学、算法爱好者的博客 176

AcWing解题目录(C++)

# 试题名称 思想方法 难度 试题来源 1 A+B 简单 AcWing 2 01背包问题 简单 3 完全背包问题 简单 4 多重背包问题I 简单 5 多重背包问题II 中等 6 多重背包问题...

Zee_Chao的博客 900

ACwing 刷题 2022/3/14

34.链表中环的入口节点 注意,我的错误在于两个指针初始化。错在指把快指针初始化成head->next->next,慢指针初始化成head,仔细想想这样相当于慢指针比快指针少走了一次!!!

weixin_53356651的博客 1874

剑指offer:对称的二叉树

对称的二叉树,注意镜像位置dfs的写法。

阿正的梦工坊 840

剑指offer

目录AcWing 13. 找出数组中重复的数字AcWing 14. 不修改数组找出重复的数字AcWing 15. 二维数组中的查找 AcWing 13. 找出数组中重复的数字 class Solution { public: int duplicateInArray(vector<int>& nums) { int n = nums.size(); for (auto x : nums) //首先遍历数组,如果存在某个数不在0到n-

HENRYyangtuo的博客 320

常用算法代码模板与题单 | D - 图论(更新于2026.10.7)

这篇文档其实是我自己学习路上的一份“复习笔记”。之前看了不少教程,零零散散记了一堆代码片段和小技巧,时间久了就容易忘,翻起来也麻烦。所以干脆花时间整理了一下,把常用的模板、容易踩的坑、还有一些个人觉得挺实用的小技巧都归到了一起。 如果你也在学类似的东西,或许能帮你省下一些翻找的时间;如果有哪里写得不清楚或不对,也欢迎随时指正交流。 希望这份整理也能对你有一点点帮助~ ✨

团爸讲算法 788

AcWing 34. 链表中环的入口结点(快慢指针赛跑+地址映射)

1.快慢指针赛跑 这是一个做起来很巧妙的算法,而且这个方法既不会改变原链表的结构,又不会需要额外的空间。 具体的操作是:从头结点开始,令快指针每次移动两个单位,慢指针每次移动一个单位。如果链表有环则两个指针必定会在环上的某一个位置相遇。此时令快指针重回起点,然后让两个指针同时每次移动一个单位,当两个指针再次相遇时(必定会相遇),相遇的结点恰好就是环的入口结点。 证明如下: 假设链表...

Zee_Chao的博客 1493

AcWing 21. 斐波那契数列(迭代算法(或DP算法))

斐波那契数列的求法有很多种,这里只列举一种最简单的方法。我们先假设斐波那契数列是如下排列的: 下标 0 1 2 3 4 5 …… 数据 0 1 1 2 3 5 …… 我们先设置两个变量f和g,并分别初始化为0和1以代表第0号元素和第1号元素。然后我们可以很容易地发现,只要不断重复进行如下操作 f ...

Zee_Chao的博客 864

AcWing 1.A+B

具体代码如下: #include <iostream> using namespace std; int main() { int a, b; cin >> a >> b; cout << a+b; return 0; }

Zee_Chao的博客 650
上一篇: 912数据结构自学习题总结Part4
下一篇: AcWing 39. 对称的二叉树
Zee_Chao
博客等级 码龄11年 168粉丝 135原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值