题目要求

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. CentOS 上开启 BBR 加速

    BBR 算法需要 Linux 4.9 及以上的内核支持,所以想要使用该方式的需要先升级内核版本. 在 Cent OS 7 上的 Linux 内核是 3.10, 使用 uname -r 查看内核版本 [ ...

  2. Windows 使用 Gogs 搭建 Git 服务器

    随便说两句 之前有使用 Gitblit 在Windows搭建Git服务器,用的也挺好的,可能安装起来略麻烦一点.现在全用 Gogs 在windows搭建Git服务器,主要是因界面好看,管理更方便一些. ...

  3. Sword STL迭代器prev,next相关函数

    迭代器的头文件中定义了4个实现迭代器模板的函数模板. .advance(iterator,num):将迭代器iterator 移动了num个位置 .distance(iterator1,iterato ...

  4. A Tour of ParallelExtensionsExtras

    Throughout the development of Parallel Extensions for the .NET Framework 4, we've come across a myri ...

  5. Numpy 定义矩阵的方法

    import numpy as np #https://www.cnblogs.com/xzcfightingup/p/7598293.html a = np.zeros((2,3),dtype=in ...

  6. 设置gem源,解决下载慢的问题

    问题解决的最好方法方法 使用google的DNS 8.8.8.8 / 8.8.4.4 另一种解决方式 修改rubygems的source源 $ gem source -r http://rubygem ...

  7. 关于SpringBoot如何返回视图

    别人已经写过了,我就不重复造轮子了.我赞成他的方案:Spring Boot使用方法小札(1):Web应用返回jsp页面 如果配置完之后,访问相应的Controller 还是得不到对应的页面,考虑用以下 ...

  8. JSON的多种转换

    String message = httpSend(url, empName, loginPassWd); // 解析json字符串 message = message.replaceAll(&quo ...

  9. 通过JVM 参数 实现spring 应用的二进制代码与配置分离。

    原创文章,转载请注明出处 分离的好处就不说了.说下分离的思路.通过JVM 参数-D 添加 config.path 的property 到系统中.系统通过System.getProperty(confi ...

  10. MySql 5.7 新特性概览

    安全的提升 1.1 在Mysql 8版本中,caching_sha2_password 是一个缺省的认证插见.5.7 版本的客户端支持 caching_sha2_password 的客户端认证. 1. ...