原题链接https://leetcode.com/problems/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. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

Example 1:

Input:
Tree 1 Tree 2
1 2
/ \ / \
3 2 1 3
/ \ \
5 4 7
Output:
Merged tree:
3
/
4 5
/ \ \
5 4 7

Note: The merging process must start from the root nodes of both trees.

思路分析

从两个树的根节点开始,对应节点相加……很朴素的一题了

源码附录

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;v1.
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if(t1==null && t2==null)
{
return null;
}
if(t1==null || t2==null)
{
return t1==null ? t2 :t1;
} t1.val = t1.val + t2.val;
t1.left = mergeTrees(t1.left,t2.left);
t1.right = mergeTrees(t1.right,t2.right);
return t1;
}
}

Merge Two Binary Trees——LeetCode进阶路的更多相关文章

  1. LeetCode 617. 合并二叉树(Merge Two Binary Trees)

    617. 合并二叉树 617. Merge Two Binary Trees 题目描述 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新 ...

  2. leetcode第一天-merge two binary trees

    有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...

  3. 【Leetcode_easy】617. Merge Two Binary Trees

    problem 617. Merge Two Binary Trees     参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完    

  4. Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees

    Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...

  5. [LeetCode] Merge Two Binary Trees 合并二叉树

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

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

  7. 【LeetCode】617. Merge Two Binary Trees 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

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

  9. LeetCode - Merge Two Binary Trees

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  10. LeetCode 617. Merge Two Binary Trees合并二叉树 (C++)

    题目: Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...

随机推荐

  1. excel 文件提示已受损 解决方案

    1.打开office excel 2.python 语言save 方法导致,将后缀改为.xls 3.用WPS打开

  2. 解决 Mac(M1/M2)芯片,使用node 14版本

    前言 nvm 在安装 Node.js v14.21.3 时,报错: nvm install 14 Downloading and installing node v14.21.3... Downloa ...

  3. nginx + lua脚本

    Nginx配合Lua 案例 今天实现一个非常简单的例子. 云服务器上部署的了一个很通用的应用程序(它没有保护策略),其端口是a,但是我想使用他,就要通过公网ip:端口去访问它.暴露在外面很不安全. 那 ...

  4. selenium爬虫学习1

    简介 Selenium是广泛使用的模拟浏览器运行的库,它是一个用于Web应用程序测试的工具. Selenium测试直接运行在浏览器中,就像真正的用户在操作一样,并且支持大多数现代 Web 浏览器. 函 ...

  5. 用于线程同步的Interlocked系列函数主要有哪些

    原子访问 通过Interlocked系列函数是 Windows API 提供的一组原子操作函数,用于在多线程环境中安全地操作共享变量.当我们执行这些Interlocked系列函数的时候 ,函数会对总线 ...

  6. 【SpringMVC】处理 JSON:使用 HttpMessageConverter

    处理 JSON:使用 HttpMessageConverter 处理 JSON 加入 jar 包: 编写目标方法,使其返回 JSON 对应的对象或集合 在方法上添加 @ResponseBody 注解 ...

  7. Linux内存管理神器:smem工具

    大家好,我是良许. 今天给大家分享一款 Linux 系统里的内存管理神器:smem . smem 是Linux系统上的一款可以生成多种内存耗用报告的命令行工具.与现有工具不一样的是 smem 可以报告 ...

  8. .net WorkFlow 流程会签

    WikeFlow官网:www.wikesoft.com WikeFlow学习版演示地址:workflow.wikesoft.com WikeFlow学习版源代码下载:https://gitee.com ...

  9. (原创)[开源][.Net Framework 4.5] SimpleMVVM(极简MVVM框架)更新 v1.1,增加NuGet包

    一.前言 意料之外,也情理之中的,在主业是传统行业的本人,技术的选型还是落后于时代. 这不,因现实需要,得将大库中的 WPF MVVM 相关部分功能拆分出来独立使用,想着来都来了,就直接开源得了,顺便 ...

  10. ElasticStack从入门到精通

    什么是ElasticStack ElasticStack早期名称为elk elk代表了三个组件 ElasticSearch 负责数据存储和检索. Logstash 负责数据的采集,将源数据采集到Ela ...