题目要求

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.

题目分析及思路

题目给出两个二叉树,要求得到一棵融合的二叉树,融合规则是有重叠的结点的值是原先结点的值的和,否则作为新树的结点。可以遍历每个结点然后如果重叠(两个二叉树结点都不为空)新结点值便为两者和,不重叠(只有一个结点为空)新结点值为不为空的值,全为空则跳出。按照这个逻辑进行迭代。

python代码​

# Definition for a binary tree node.

# class TreeNode:

#     def __init__(self, x):

#         self.val = x

#         self.left = None

#         self.right = None

class Solution:

def mergeTrees(self, t1, t2):

"""

:type t1: TreeNode

:type t2: TreeNode

:rtype: TreeNode

"""

if t1 is None and t2 is None:

return

if t1 is None:

return t2

if t2 is None:

return t1

t1.val += t2.val

t1.left = self.mergeTrees(t1.left,t2.left)

t1.right = self.mergeTrees(t1.right,t2.right)

return t1

LeetCode 617 Merge Two Binary Trees 解题报告的更多相关文章

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

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

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

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

  4. Leetcode 617 Merge Two Binary Trees 二叉树

    题意: 给定两棵树,将两棵树合并成一颗树 输入 Tree 1 Tree 2 1 2 / \ / \ 3 2 1 3 / \ \ 5 4 7 输出 合并的树 3 / \ 4 5 / \ \ 5 4 7 ...

  5. 【Leetcode_easy】617. Merge Two Binary Trees

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

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

  7. 【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)

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

  8. 【LeetCode】894. All Possible Full Binary Trees 解题报告(Python & C++)

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

  9. [LeetCode&Python] Problem 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 ...

随机推荐

  1. D3.js学习

    // 1.选择d3.select('p')d3.selectAll('p')d3.select('.txt').style('color', '#fff')// 2.支持动态设置属性// a:随机属性 ...

  2. 【Java】加载驱动方法

    1.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 2. DriverManager.register ...

  3. 【30集iCore3_ADP出厂源代码(ARM部分)讲解视频】30-10底层驱动之I2C

    视频简介:该视频介绍iCore3应用开发平台中I2C通信的实现方法. 源视频包下载地址:链接:http://pan.baidu.com/s/1dF5Ssbn 密码:czw8 银杏科技优酷视频发布区:h ...

  4. NodeJS + PhantomJS 抓取页面信息以及截图

    利用PhantomJS做网页截图经济适用,但其API较少,做其他功能就比较吃力了.例如,其自带的Web Server Mongoose最高只能同时支持10个请求,指望他能独立成为一个服务是不怎么实际的 ...

  5. Maven手动安装jar包到仓库

    mvn install:install-file -Dfile=C:\Users\Administrator\Desktop\IKAnalyzer6.5.0.jar -DgroupId=com.luc ...

  6. 在linux下用命令行编译 java的eclipse项目

    由于jdk的版本问题导致在windows上编译打包好的jar包放在linux服务器上运行的时候出现一点小异常,所以决定在linux上进行一次项目编译,这有两个选择1.在相同的linux环境下安装lin ...

  7. Docker入门学习总结

    1. 什么是Docker Docker是一种虚拟化技术,其在容器的基础上进一步封装了文件系统.网络互联.进程隔离等等,从而极大地简化了容器的创建和维护.Docker使用 Google 公司推出的 Go ...

  8. 解决Hive与Elasticsearch共有库 guava 冲突 NoSuchMethodError

    情况描述 解决方法 方法一:Shade and relocate 简介 Shade Elasticsearch 引入shade ES jar 方法二:修改集群Job配置策略(未实验) 情况描述 使用J ...

  9. Ubuntu16.04安装xgboost

    1.Python下安装方法 git clone --recursive https://github.com/dmlc/xgboost cd xgboost make -j4 cd python-pa ...

  10. 微信小程序中使用Async-await方法异步请求变为同步请求

    微信小程序中有些 Api 是异步的,无法直接进行同步处理.例如:wx.request.wx.showToast.wx.showLoading等.如果需要同步处理,可以使用如下方法: 注意: Async ...