[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 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的更多相关文章
- 【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 ...
 - [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 ...
 - 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 ...
 - 【LeetCode】617. Merge Two Binary Trees 解题报告
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
 - 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 ...
 - 【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 ...
 - 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 ...
 - [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 ...
 
随机推荐
- linux 系统调用号表
			
位于 /usr/include/asm/unistd.h 由于我是64位系统,所以有一些额外的东西.我的这个文件为下文 #ifndef _ASM_X86_UNISTD_H #define _ASM_X ...
 - Flutter学习笔记(一)
			
记得flutter出来的时候,官方推荐的是使用IntelliJ IDEA,当时个人尝试了一下,比较麻烦,整个过程比较漫长. 进入2018年,再去看的时候,官方推荐使用Android Studio和VS ...
 - resin中关于url rewrite来传递jsessionid的问题
			
最近两天在项目中碰到,一个很奇怪的问题.同一个账号多次切换登录时,会出现这个账号的信息在session中找不到,虽然可以登录成功,但是之后这个用户信息好像没有保存到session中一样,或者是被改变了 ...
 - Python mysql-表中数据的大量插入
			
2017-09-06 23:28:26 import pymysql db = pymysql.connect("localhost","root"," ...
 - C#异常信息获取
			
try { ; / i; } catch (Exception ex) { /** * 1.异常消息 * 2.异常模块名称 * 3.异常方法名称 * 4.异常行号 */ String str = &q ...
 - ACM/ICPC 2018亚洲区预选赛北京赛站网络赛
			
题意:到一个城市得钱,离开要花钱.开始时有现金.城市是环形的,问从哪个开始,能在途中任意时刻金钱>=0; 一个开始指针i,一个结尾指针j.指示一个区间.如果符合条件++j,并将收益加入sum中( ...
 - Android之SwipeRefreshLayout下拉刷新组件
			
SwipeRefreshLayout概述 SwipeRefrshLayout是Google官方更新的一个Widget,可以实现下拉刷新的效果.该控件集成自ViewGroup在support-v4兼容包 ...
 - JSP页面出现乱码
			
Jsp文件中会出现下面所示的编码指定方式: <%@ page language="java" contentType="text/html; charset=UTF ...
 - UVA557 汉堡 Burger
			
题面 https://www.luogu.org/problemnew/show/UVA557 这里顺便整理一下二维格点随机游走问题. 遇到这种问题时,需注意分母的计算问题. 设x为起点到终点的距离. ...
 - mysql日期查询大全
			
-- 查询昨日一整天的数据 DAY) ,'%Y-%m-%d 23:59:59') AS '昨日结束时间' -- 查询今日开始到当前时间的数据 DAY) ,'%Y-%m-%d %H:%i:%s') AS ...