leetcode(617):Merge Two Binary Trees

news/2024/7/7 4:09:04 标签: leetcode

题目要求:
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.
这里写图片描述

题目分析:
合并二棵树,其实就是遍历的问题,采用先序遍历,先访问根结点,然后访问左子结点,右子结点,采用递归的方式来实现,对于递归肯定是有结束的判断,对于结束的条件返回什么比较合算,需要在不同的题中,不同的分析。在本题中,结束返回的是处理完的那个节点,然后之前的结点的左子结点,右子结点就可以为其赋值,达到想要的结果。
python代码的实现:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def mergeTrees(self, t1, t2):
        """
        :type t1: TreeNode
        :type t2: TreeNode
        :rtype: TreeNode
        """
        if t1 != None and t2 != None:
            t1.val = t1.val + t2.val
        if t1 == None and t2 != None:
            return t2
        if t1 != None and t2 == None:
            return t1
        if t1 == None and t2 == None:
            return t1
        t1_l = t1.left
        t2_l = t2.left
        node = self.mergeTrees(t1_l, t2_l)
        t1.left = node
        t1_r = t1.right
        t2_r = t2.right
        node = self.mergeTrees(t1_r, t2_r)
        t1.right = node
        return t1

再重新写一遍程序,发现之前的只是实现功能,改变了原有的树的结构,这并不是我们愿意看到的景象,所以,应该是新建节点,然后建立新的树。其实下面的代码也是改变了结构,改了不相匹配的节点(一个为空,一个不为空,改变了不为空的那个节点)
python代码:

    def mergeTrees(self, t1, t2):
        """
        :type t1: TreeNode
        :type t2: TreeNode
        :rtype: TreeNode
        """
        if t1 and t2:
            new_node = TreeNode(t1.val + t2.val)
            new_node.left = self.mergeTrees(t1.left, t2.left)
            new_node.right = self.mergeTrees(t1.right, t2.right)
            return new_node
        return t1 or t2

先来看看大神们是怎么做的。大佬真的是厉害,通过判断,为空的变化为0来做,然后就可以把语句整合到一起了。

def mergeTrees(self, t1, t2):
    if not t1 and not t2: return None
    ans = TreeNode((t1.val if t1 else 0) + (t2.val if t2 else 0))
    ans.left = self.mergeTrees(t1 and t1.left, t2 and t2.left)
    ans.right = self.mergeTrees(t1 and t1.right, t2 and t2.right)
    return ans

我刚刚一晃神,以为是自己写的程序,,哇,原来也是之前复制粘贴大佬的程序,居然跟我写的一样,尴尬,保证没有抄袭。。

class Solution(object):
    def mergeTrees(self, t1, t2):
        if t1 and t2:
            root = TreeNode(t1.val + t2.val)
            root.left = self.mergeTrees(t1.left, t2.left)
            root.right = self.mergeTrees(t1.right, t2.right)
            return root
        else:
            return t1 or t2
class Solution(object):
    def mergeTrees(self, t1, t2):
        """
        :type t1: TreeNode
        :type t2: TreeNode
        :rtype: TreeNode
        """
        if not t1 and not t2: return None
        if t1:
            v1, L1, R1 = t1.val, t1.left, t1.right
        else:
            v1, L1, R1 = 0, None, None
        if t2:
            v2, L2, R2 = t2.val, t2.left, t2.right
        else:
            v2, L2, R2 = 0, None, None
        node = TreeNode(v1+v2)
        node.left = self.mergeTrees(L1, L2)
        node.right = self.mergeTrees(R1, R2)
        return node

大佬另辟蹊径,然后把类别分出来,然后依据类别进行选择。。。666.


http://www.niftyadmin.cn/n/1798843.html

相关文章

POJ 1247 数组首尾求和判定相等 枚举法

此题把题意弄懂后就很容易&#xff0c;S顺时针走&#xff0c;E逆时针走&#xff0c;确定一个分割点该处两人相遇时途经的客人数之和相等&#xff0c;直接枚举。 #include <iostream> using namespace std; //s顺时针走&#xff0c;e逆时针走&#xff0c;问他们在哪里相遇…

开源 免费 java CMS - FreeCMS1.8 互动信件

2019独角兽企业重金招聘Python工程师标准>>> 项目地址&#xff1a;http://code.google.com/p/freecms/ 互动信件 1. 部门信件管理 部门信件指接收人为部门的信件,从左侧管理菜单点击部门信件进入。 admin可以管理所有部门信件&#xff0c;其他用户可以管理自己…

leetcode(669):Trim a Binary Search Tree

题目&#xff1a;Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R > L). You might need to change the root of the tree, so the result should return the new root of the tr…

Linux-nat与nat

一、Linux-nat1、SANT应用环境&#xff1a;2、SNAT原理图3、SNAT实现步骤4、网关使用动态公网IP5、DNAT应用环境&#xff1a;6、DNAT原理7、DNAT使用步骤8、使用DNAT修改端口号二、真实路由器-nat1、分类&#xff1a;a、静态NAT(一个内部地址对应一个唯一的外部地址&#xff0c…

C++ STL string类相关

之所以抛弃char*的字符串而选用C标准程序库中的string类&#xff0c;是因为他和前者比较起来&#xff0c;不必 担心内存是否足够、字符串长度等等&#xff0c;而且作为一个类出现&#xff0c;他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 进行赋值…

leetcode(404):Sum of Left Leaves

题目&#xff1a;Find the sum of all left leaves in a given binary tree. example: 3 / \ 9 20 / \ 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 题目分析&#xff1a;求所有左叶子节点的和&#xff0c;然后…

WPF支持GIF的各种方法

2012.12.18更新&#xff1a;修复下载链接 已知WPF的Image元素只能显示GIF图片的第一帧&#xff0c;而MediaElement不能加载作为资源或内嵌的资源的GIF图片&#xff0c;所以网上有几种实现方法。 我抄袭网上提供的方法&#xff0c;改头换面后作为自己的GifImage实现。本文的前半…

简单认识LVS-DR负载群集和部署实例

文章目录 一、LVS-DR负载群集简介1、DR模式数据包流向分析2、DR 模式的特点 二、DR模式 LVS负载均衡群集部署 一、LVS-DR负载群集简介 1、DR模式数据包流向分析 1、客户端发送请求到 Director Server&#xff08;负载均衡器&#xff09;&#xff0c;请求的数据报文&#xff0…