"""
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. Checked exceptions: Java’s biggest mistake-检查型异常:Java最大的错误(翻译)

    原文地址:http://literatejava.com/exceptions/checked-exceptions-javas-biggest-mistake/ 仅供参考,毕竟我四级都没过 Chec ...

  2. Linux内核5.4正式将华为EROFS超级文件系统合入主线

    导读 近期,Linux内核5.4系列宣布全面可用,添加了许多新功能,更强的安全性和更新的驱动程序,以提供更好的硬件支持.Linux内核5.4增加对微软exFAT文件系统的支持,另外还支持内核锁定功能, ...

  3. Python 之网络编程之进程总体概要

     一: 进程的概念:(Process) 进程就是正在运行的程序,它是操作系统中,资源分配的最小单位. 资源分配:分配的是cpu和内存等物理资源 进程号是进程的唯一标识 同一个程序执行两次之后是两个进程 ...

  4. NetCore Web项目目录结构说明

    目录结构说明 目录/文件 说明 依赖项 ASP.NET Core 开发.构建和运行过程中的依赖想,一般都是 NuGet 包和一些 SDK Properties 配置,存放了一些 .json 文件用于配 ...

  5. Spark教程——(4)Spark-shell调用SQLContext(HiveContext)

    启动Spark-shell: [root@node1 ~]# spark-shell Setting default log level to "WARN". To adjust ...

  6. idea中的Storm1.1.1工程自定义日志级别

    在idea中跑storm工程时,因为Storm中的日志级级别默认为INFO,控制台总是打印出很多没用的INFO级别的日志,导致我自己在代码中的sout内容看不清楚. 于是想着自定义日志的打印级别为WA ...

  7. 第1节 storm编程:3、storm的架构模型的介绍

    nimbus:主节点,接收客户端提交的任务,并且分配任务,新的版本当中nimbus已经可以有多个了 zookeeper集群:storm依靠zk来保存一些节点信息,nimbus将分配的任务信息都写入到z ...

  8. Android 隐藏手机号中间四位和隐藏邮箱地址中间四位

    /** * 手机号用****号隐藏中间数字 * * @param phone * @return */public static String settingphone(String phone) { ...

  9. MAC 安装 pygraphviz 找不到头文件

    networkx的有向图只能通过箭头来区别两点之间的两条边,但是我在复现snake论文的时候,需要绘制两个交叉口之间的两条不同方向的路段,最后选择了pygraphviz 直接通过anaconda打开对 ...

  10. Linux centosVMware vim 编辑模式、vim命令模式、vim实践

    一.编辑模式.命令模式 在一般模式下输入:或/可进入命令模式.在该模式下可进行走索某个字符或字符串,也可保存.替换.退出.显示行号等. /word:在光标之后查找一个字符串word,按n向后继续搜索 ...