leetcode(617). Merge Two Binary Trees
来源:互联网 发布:如何应对压力 知乎 编辑:程序博客网 时间:2024/05/17 03:56
problem
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.
分析
题目的意思就是把两个二叉树的对应的位置相加。所以这里我选择了递归的方法,代码如下:
# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def mergeTrees(self, t1, t2): """ :type t1: TreeNode :type t2: TreeNode :rtype: TreeNode """ if t1 != None and t2 != None: t1.val += t2.val t1.left = self.mergeTrees(t1.left, t2.left) t1.right = self.mergeTrees(t1.right, t2.right) return t1 elif t1 != None and t2 == None: return t1 elif t1 == None and t2 != None: return t2 else: return None
阅读全文
0 0
- leetcode 617 merge two binary trees
- 【LeetCode】617 Merge Two Binary Trees
- leetcode 617:Merge Two Binary Trees
- leetcode(617). Merge Two Binary Trees
- LeetCode(617) Merge Two Binary Trees
- 【Leetcode-easy-617】Merge Two Binary Trees
- 【leetcode】 Merge Two Binary Trees
- leetcode[Merge Two Binary Trees]
- Merge Two Binary Trees(leetcode)
- The Solution to Leetcode 617 Merge Two Binary Trees
- [leetcode]617. Merge Two Binary Trees
- leetcode.617.Merge Two Binary Trees
- [leetcode]: 617. Merge Two Binary Trees
- [Leetcode]617. Merge Two Binary Trees
- LeetCode 617. Merge Two Binary Trees (C++)
- LeetCode 617. Merge Two Binary Trees
- [LeetCode] 617. Merge Two Binary Trees
- LeetCode 617. Merge Two Binary Trees
- Servlet--Servlet转发和重定向
- 设计模式之类对象结构型 — ADAPTER (适配器)模式
- kubernetes学习记录(2)——基于Centos7.2的kubernetes集群自动安装与配置
- C语言之变量(笔记四)
- maven(二)
- leetcode(617). Merge Two Binary Trees
- android killer插入log打印加密字符串
- poj 3070 Fibonacci
- ionic 自定义插件 使用.aar
- mysql 5.7 不能正常显示错误信息
- 设计模式之单例模式
- natapp 配置微信小程序开发需要的网络环境
- 对称排序
- Linux中more和less命令用法