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 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的更多相关文章
- Leetcode617.Merge Two Binary Trees合并二叉树
给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 ...
- LeetCode 617. 合并二叉树(Merge Two Binary Trees)
617. 合并二叉树 617. Merge Two Binary Trees 题目描述 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新 ...
- leetcode第一天-merge two binary trees
有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...
- 【Leetcode_easy】617. Merge Two Binary Trees
problem 617. Merge Two Binary Trees 参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- 【pwnable.kr】 asm
一道写shellcode的题目, #include <stdio.h> #include <string.h> #include <stdlib.h> #inclu ...
- java连接sql server 2008
请先确保已经设置好了sa,如果不是,可以参照下面链接修改http://jingyan.baidu.com/article/8cdccae9452b3c315513cd52.html 然后重启数据库,重 ...
- [Linux] day07——查看及过滤文本
查看及过滤文本 =====================================cat concatenate -n 添加行号------------------- ...
- NetCore Web项目目录结构说明
目录结构说明 目录/文件 说明 依赖项 ASP.NET Core 开发.构建和运行过程中的依赖想,一般都是 NuGet 包和一些 SDK Properties 配置,存放了一些 .json 文件用于配 ...
- postman提交文件
说明 1.Headers中添加 Content-Type multipart/form-data 2.Body 中选择form-data 并添加 需要传的参数名和值 最后新的一行选择file ...
- MySQL帮助文档的使用
帮助文档使用 在 MySQL 使用过程中,可能经常会遇到以下问题: 某个操作语法忘记了,需要快速查找. 当前版本上,某个字段类型我们想快速知道它的取值范围? 当前版本上,都支持哪些函数?希望有例子能快 ...
- 【剑指Offer面试编程题】题目1520:树的子结构--九度OJ
题目描述: 输入两颗二叉树A,B,判断B是不是A的子结构. 输入: 输入可能包含多个测试样例,输入以EOF结束. 对于每个测试案例,输入的第一行一个整数n,m(1<=n<=1000,1&l ...
- 夯实Java基础(十五)——Java中Comparable和Comparator
1.前言 对于Java中的对象,我们只能使用基本运算符==.!=来判断一下地址是否相等,不能使用>.<来比较大小.但是在实际的开发中,我们需要对对象进行排序,也就是比较大小,那么应该如何实 ...
- Django:使用django自带的登录模块登录后会默认登录到 /accounts/profile 下的问题
django settings中LOGIN_REDIRECT_URL默认重定向到/accounts/profile下,可通过配置修改
- 一、jsp和Servlet基础理论及jstl和EL表达式用法
1.题外话:使用JSP有近一年半的时间了,但是开发量不大.昨天部门突然让做个读取EXCLE文件,然后在前台页面进行展示.并通过点击查看按钮可以对每条明细记录进行跳转后进行详情查看,并按照页面原型进行页 ...