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.
# 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==None and t2!=None:
return t2
elif t1!=None and t2==None:
return t1
elif t1==None and t2==None:
return None
else:
def submergetree(r1,r2):
if r1!=None and r2==None:
return r1
elif r1==None and r2!=None:
return r2
elif r1==None and r2==None:
return None
else:
r1.val+=r2.val
r1.left=submergetree(r1.left,r2.left)
r1.right=submergetree(r1.right,r2.right)
return r1 return submergetree(t1,t2)

  

[LeetCode&Python] Problem 617. Merge Two Binary Trees的更多相关文章

  1. 【Leetcode_easy】617. Merge Two Binary Trees

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

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

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

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

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

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

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

  7. 【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 o ...

  8. 617. Merge Two Binary Trees(Easy)

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

  9. [Algorithm] 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. 已使用 163 邮箱测试通过,且支持 SSL 连接。 发送邮件

    示例:Jack 发送一封邮件给 Rose. public class SendMail {     public static void main(String[] args) {         b ...

  2. jsp session/application

    session的增加读取 session.setAttribute("username", "张三"); String u = (String) session ...

  3. Illumina Sequence Identifiers 序列标识符 index详解

    大家基本都知道什么是 FASTA 和 FastQ 格式了,但这是不够的. 我们还需要了解世界上最大的测序公司自己定制的 FastQ 格式,因为你可能会经常用到,有时还会亲自去处理它们. 本文主题:Il ...

  4. 20170728xlVba SSC_LastTwoDays

    Public Sub SSCLastTwoDays() Dim strText As String Dim Reg As Object, Mh As Object, OneMh As Object D ...

  5. Mysql批量导入约束报错

    SET foreign_key_checks = 0;   禁用外键,在文件顶部加 SOURCE dump_file_name;      进行SQL执行 SET foreign_key_checks ...

  6. IE6不兼容postion:fixed已解决

    将要设置postion:fixed的元素的css中添加以下代码: //如果想要是头部悬停,则以下代码即可. .customService{        position: fixed;bottom: ...

  7. 秒杀多线程第三篇 原子操作 Interlocked系列函数

    上一篇<多线程第一次亲密接触 CreateThread与_beginthreadex本质区别>中讲到一个多线程报数功能.为了描述方便和代码简洁起见,我们可以只输出最后的报数结果来观察程序是 ...

  8. nyoj1272表达式求值(递归法)

    递归写的,类似于之前的一道,但要更难一点,因为加入了'+','*'以及括号运算符,所以要考录周全: 这道题给了我很大启示,在第一道德基础上: 1!寻找括号的优先级,先找有没有不被任何括号夹住的加号,如 ...

  9. 第 4 章—— C# 语言特性(《精通 ASP.NET MVC 5》)

    这里只提供各个特性的简单概括. C# 的完整指南可参阅<Introducing Visual C#>.深度了解 LINQ 可参考<Pro LINQ in C#> 4.1 准备示 ...

  10. iOS UI-创建空项目

    一.创建工程 二.删除ViewController 三.在Supporting Files/Info.plist文件中清空值 四.创建新的控制器 五.写代码 1.在AppDelegate.h文件中 # ...