"""
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
"""
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution:
def mergeTrees(self, t1: TreeNode, t2: TreeNode) -> TreeNode:
if t1 == None:
return t2
if t2 == None:
return t1
if t1 and t2 == None:
return None
result = TreeNode(t1.val + t2.val)
result.left = self.mergeTrees(t1.left, t2.left)
result.right = self.mergeTrees(t1.right, t2.right)
return result
# Solution2
# t1.val += t2.val
# t1.left = self.mergeTrees(t1.left, t2.left)
# t1.right = self.mergeTrees(t1.right, t2.right)
# return t1
"""
此题尚未实现迭代法,但可以省去result空间
"""

leetcode617 Merge Two Binary Trees的更多相关文章

  1. Leetcode617.Merge Two Binary Trees合并二叉树

    给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 ...

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

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

  3. leetcode第一天-merge two binary trees

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

  4. 【Leetcode_easy】617. Merge Two Binary Trees

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

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

  6. [Swift]LeetCode617. 合并二叉树 | 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. 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 ...

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

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

随机推荐

  1. 解决EFCore缓存机制导致的数据查询错误问题

    如题,在对同一个Context连续进行相同条件的查询时,会触发EFCore的缓存机制,如果这个过程中数据发生了变化,则会出现错误. 例如:有两个Context实例,一个负责查询,一个负责增删改, A_ ...

  2. 第2节 Scala中面向对象编程:7、继承的概念以及override和super关键字;8、isInstanceOf 和 asInstanceOf关键字

    6.3.   Scala面向对象编程之继承 6.3.1.     Scala中继承(extends)的概念 Scala 中,让子类继承父类,与 Java 一样,也是使用 extends 关键字: 继承 ...

  3. winform和wpf里必知的多线程知识

    背景: 很多小伙伴经常在群里问线程的问题,平时我经常转一些视频教程这些人不看,我就自己写个总结吧 不过还是要注意的是,切换本来就不能太频繁,要一口气改. wpf的viewmodel就不需要UI线程,更 ...

  4. SpringBoot 集成MyBatis、事务管理

    集成MyBatis (1)在pom.xml中添加依赖 <!-- mybatis的起步依赖.包含了mybatis.mybatis-spring.spring-jdbc(事务要用到)的坐标 --&g ...

  5. 搭建 nginx + rtmp 媒体服务器笔记

    工作需要搭建一个流媒体服务器,用来接收前端推过来的视频流,达到实时保存的目的. 具体步骤网上已经比较详细了 可以参考下面这个文档参考文档 https://www.cnblogs.com/monjeo/ ...

  6. egret inspector插件无法使用

    调试项目要安装egret inspector查看游戏场景的资源,装了插件点击不显示. 解决方法:将chrome版本回退. 下载地址:http://mydown.yesky.com/pcsoft/279 ...

  7. 多年珍藏的55w御剑字典

    御剑珍藏55w目录字典,很给力,放在以前直接数据库都能给跑出来. 用法:直接把放入配置文件的目录 链接:https://pan.baidu.com/s/1MGxdd9hH006Y7AO7CpkO8g ...

  8. CNN反向传播算法公式

    网络结构(6c-2s-12c-2s): 初始化: \begin{align}\notag W \sim U(- \frac{\sqrt{6}}{\sqrt{n_j+n_{j+1}}} , \frac{ ...

  9. IDEA中maven工程打包时使用跳过test模式

  10. HHvm Apache 2.4 Nginx建站环境搭建方法安装运行WordPress博客

    HHvm Apache 2.4 Nginx建站环境搭建方法安装运行WordPress博客 VPS主机   2014年06月02日 17:20   评论»       文章目录 Debian上安装 Ce ...