LeetCode 617. Merge Two Binary Trees

FPGA:UART串口接收(高干扰情况) 在上一篇文章中介绍过UART串口接收模块,其中一个需要优化的方向就是在高干扰情况下,如何实现可靠的UART串口接收任务。 阅读详情

题目链接:https://leetcode.com/problems/merge-two-binary-trees/description/

题目解析:将T1作为模板,把T2合进来。容易想到的是用递归的方法遍历子树,主要分成三种情况——

  • T1和T2当前节点都存在,那么两者值相加即可,继续分别递归左右子树;
  • T1不存在,T2存在,那么把T1当前空节点赋值为T2当前节点即可,以T2当前节点为根结点的子树自然会合并到T1上;
  • T1存在,T2不存在,那么只要返回即可,不用进行任何操作。

代码如下:20ms Accepted beating 100%

/**
 * 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:
    TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
        if (t1 != NULL && t2 != NULL)
        {
            t1->val = t1->val + t2->val;
            t1->right = mergeTrees(t1->right, t2->right);
            t1->left = mergeTrees(t1->left, t2->left);
            return t1;
        }
        else if (t2 != NULL)
        {
            t1 = t2;
            return t1;
        }
        else
            return t1;
    }
};

static const auto ____ = [] () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    return nullptr;
}();

 

【测开求职】面试题:数据结构 吐血整理 本篇文章是笔者为准备研究生复试以及校招而写,因此主要针对问答,注重理解,而不在于细节计算。涵盖考研范围内的所有知识内容,读者可以先查看上面的链接走一遍数据结构的整体框架结构。其中,重点难点以及易混淆的知识点,笔者用其他近30篇文章进行了详述,在知识点处贴好了链接,有需要的可以查阅。 阅读详情

相关推荐

MPU6050芯片 寄存器详解与配置指南(详解)

分频值越小,数据刷新越快(如分频值 = 0 时,刷新率 = 8kHz/1=8kHz)。功能:配置采样频率分频系数,控制 ADC 转换速度和数据刷新率。数据刷新率 = 陀螺仪输出时钟频率 /(1 + 分频值)

m0_74696257的博客 1425

Leetcode学习笔记:#617. Merge Two Binary Trees

Leetcode学习笔记:#617. Merge Two Binary Trees Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You ...

ccystewart的博客 155

Ubuntu双系统引导修复实战:boot-repair工具详解

本文详细介绍了使用boot-repair工具修复Ubuntu双系统引导丢失问题的实战方法。当GRUB引导菜单因安装新系统或更新而损坏时,通过制作Ubuntu启动盘进入Live环境,安装并运行boot-repair,可一键完成系统修复引导,自动化执行GRUB重装与配置,解决无法进入Windows或Ubuntu的常见故障。

weixin_29224589的博客 360

[Leetcode]617. Merge Two Binary Trees

[Leetcode]617. Merge Two Binary Trees

正西风落叶下长安 1749

iSLIP算法实战:如何用Python模拟交换机优先级匹配(附完整代码)

本文详细介绍了如何使用Python实现iSLIP算法,模拟交换机优先级匹配过程。通过核心的指针滑动逻辑和迭代循环策略,构建了一个完整的iSLIP算法模拟器,并加入可视化模块以直观展示匹配过程。文章还提供了完整的代码框架和性能优化建议,适合网络工程师和Python开发者直接集成到测试环境中。

weixin_29313239的博客 253

通达信股本变迁文件(gbbq)解密方法

近年来A股市场跌宕起伏真特么精彩,本人愚钝,被整爆仓,损失惨重 痛定思痛,认为交易这种对纪律要求极高,且必须完全排除情绪影响的勾当,还是应该交给程序来干 数据哪里来呢?当然是拿来主义。。。问券商的交易软件要呗 到处查资料,得知通达信的权息文件数据齐全,不仅含有除权除息数据,还含有限售解禁、增发、可转债上市等股本变化数据 ——这对于某些对成交量变化敏感的交易模型是非常重要的

不甘碌碌老,结庐铸宝刀 2万+

leetcode 617. Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tr

淡然坊 497

【AI应用实战-WorkBuddy】WorkBuddy 能帮你做什么(一)

文章摘要: WorkBuddy是一款AI工作助手,专为提升效率而设计,适用于经常处理文档、代码、数据分析等重复性任务的职场人士。它能通过自然语言交互快速完成五大核心场景任务: 周报撰写:输入工作要点,10分钟生成专业周报; 代码调试:解析报错信息,3分钟提供解决方案; Excel分析:自动计算数据趋势并生成图表和结论; 自动化脚本:将重复流程转为脚本,节省98%时间; 文档生成:根据代码自动补充README、API文档和注释。 效果对比:传统手动操作耗时1-3小时的任务,WorkBuddy可将时间缩短至10

353

LeetCode //C - 617. Merge Two Binary Trees

【代码】LeetCode //C - 617. Merge Two Binary Trees。

Made in Code 1134

【华为OD机试】真题A卷-Linux发行版的数量(Python)

【华为OD机试】真题A卷-Linux发行版的数量(Python):Linux操作系统有多个发行版,distrowatch.com提供了各个发行版的资料。这些发行版互相存在关联,例如Ubuntu基于Debian开发,而Mint又基于Ubuntu开发,那么我们认为Mint同Debian也存在关联。 发行版集是一个或多个相关存在关联的操作系统发行版,集合内不包含没有关联的发行版。给你一个nxn的矩阵isConnected,其中isConnected[i][i]=1表示第i个发行版和第j个发行版直接

u014481728的博客 944

解锁激光加工密码:功率、频率、脉宽、占空比全解析

激光加工利用高能量激光束实现材料加工,具有无接触、高效率等特点。其核心参数包括功率(决定能量输出)、频率(控制脉冲重复)、脉宽(调节能量作用时间)和占空比(调控工作周期),这些参数相互协同影响加工效果。针对不同材料需采用差异化参数组合:金属材料需较高功率,塑料需低功率防过热,陶瓷需高频短脉宽减少热应力。实际操作中应通过小范围测试逐步优化参数,并注意解决烧损、切割不平整等常见问题。合理调节这些参数可实现高质量、高效率的激光加工。

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 618

【LeetCode】617. Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree...

韩旭051的博客 636

论文阅读:arixv 2025 Poisoning Attacks on LLMs Require a Near-constant Number of Poison Samples

研究者做了目前最大规模的“投毒实验”,训练了从6亿参数到130亿参数的模型(覆盖了中小到大型LLM),用的训练数据量也是“最适合模型大小”的(按行业里的“Chinchilla法则”,模型越大,训练数据量也按比例增加)。总结下来就是:这篇论文告诉我们,给大语言模型投毒,比之前想的容易得多——不需要海量毒数据,只要几百条藏好后门的文档,就能让大模型“带毒工作”,而且平时还看不出来。——这比之前大家担心的“需要投毒数据占训练集一定比例”要可怕得多,因为对大模型来说,“固定数量”的毒数据更容易偷偷塞进去。

CSPhD-winston的博客 1081

Leetcode 617. Merge Two Binary Trees

Leetcode 617. Merge Two Binary Trees Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need t...

fengran_mark的博客 225

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

OV9734 DATASHEET,完整规格书

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

[LeetCode] 617. Merge Two Binary Trees

原题链接:https://leetcode.com/problems/merge-two-binary-trees/ 1. 题目介绍 Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while...

Ber03的博客 218

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

【Leetcode 617】Merge Two Binary Trees -EASY

【Leetcode 617】Merge Two Binary Trees - EASY题目示例: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190316201010341.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLm...

weixin_43621681的博客 389

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

Leetcode——617. Merge Two Binary Trees

题目原址 https://leetcode.com/problems/merge-two-binary-trees/description/ 题目描述 Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are over...

想当厨子的程序媛 335

Leetcode 617 Merge Two Binary Trees

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { pub...

Polanwind 114

LeetCode: 617. Merge Two Binary Trees

LeetCode: 617. Merge Two Binary Trees Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

qha0731的专栏 326

LeetCode:617. Merge Two Binary Trees

LeetCode:617. Merge Two Binary Trees Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need...

Wisimer 197

617. Merge Two Binary Trees python leetcode

617. Merge Two Binary TreesDescriptionHintsSubmissionsDiscussSolutionPick OneGiven two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overla...

一名路过的小码农啊 622

[LeetCode] 617.Merge Two Binary Trees

[LeetCode] 617.Merge Two Binary Trees 题目描述 解题思路 实验代码 题目描述Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others

user_name_is_so_long的博客 234

leetcode.617.Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

RaymondWong的博客 839

Leetcode617. Merge Two Binary Trees (C++)

Leetcode617. Merge Two Binary Trees 简单题,迭代比递归复杂一点点 代码如下: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeN

无远弗届 252

Leetcode 617. Merge Two Binary Trees(python)

Leetcode 617. Merge Two Binary Trees题目解法1:DFS解法2:BFS 题目 解法1:DFS 在recursion的每一步 如果当前的两个节点都不为空,更新第一棵树的当前节点val为两个节点val的和 如果其中有一个节点为空,则返回另一个节点 当前节点的left和right节点为下一层recursion返回的结果 class Solution(object)...

qq_37821701的博客 390

python leetcode 617. Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a new binary tree. ...

DreamerLHQ的博客 681
上一篇: LeetCode 852. Peak Index in a Mountain Array
下一篇: LeetCode 654. Maximum Binary Tree(O(n)方法!)
ZayneHuang
博客等级 码龄10年 5粉丝 116原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值