leetcode(572):Subtree of Another Tree

题目要求:
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node’s descendants. The tree s could also be considered as a subtree of itself.
Example 1:
Given tree s and t:

 s   3
    / \
   4   5
  / \
 1   2

 t 4 
  / \
 1   2

Return true, because t has the same structure and node values with a subtree of s.

 s   3
    / \
   4   5
  / \
 1   2
    /
   0

t  4   
  / \
 1   2

Return false.

题目分析:这个是来是否存在子模式的问题,在这个问题中,需要从根节点开始判断是否与模式树完全相同,完全相同,返回True,否则的话,判断左右子树与模式树是否完全相同,然后返回值。注意对于None节点的判断,因为在递归中可能会出现None节点的情况,所以必须要考虑。

python代码实现:

class Solution(object):
    def isSubtree(self, s, t):
        """
        :type s: TreeNode
        :type t: TreeNode
        :rtype: bool
        """
        if not s:
            return False
        return self.is_same(s, t) or self.isSubtree(s.left, t) or self.isSubtree(s.right, t)


    def is_same(self, s, t):
        if s is None and t is None:
            return True
        if s is None or t is None:
            return False
        if s.val != t.val:
            return False       
        return self.is_same(s.left, t.left) and self.is_same(s.right, t.right)

看一下大佬是怎么实现的。
大佬用原始的朴素的方法来做,此时算法复杂度为O(|s|*|t|).

def isMatch(self, s, t):
    if not(s and t):
        return s is t
    return (s.val == t.val and 
            self.isMatch(s.left, t.left) and 
            self.isMatch(s.right, t.right))

def isSubtree(self, s, t):
    if self.isMatch(s, t): return True
    if not s: return False
    return self.isSubtree(s.left, t) or self.isSubtree(s.right, t)

好吧,大佬不好的方法与我的方法是类似的,但大佬厉害的地方在于对于None的判断那个地方,可以学习一波操作。我也可以对多个返回值能放在一起的,放在一起来书写。

看一下大佬的复杂的程序是什么样的

def isSubtree(self, s, t):
    from hashlib import sha256
    def hash_(x):
        S = sha256()
        S.update(x)
        return S.hexdigest()

    def merkle(node):
        if not node:
            return '#'
        m_left = merkle(node.left)
        m_right = merkle(node.right)
        node.merkle = hash_(m_left + str(node.val) + m_right)
        return node.merkle

    merkle(s)
    merkle(t)
    def dfs(node):
        if not node:
            return False
        return (node.merkle == t.merkle or 
                dfs(node.left) or dfs(node.right))

    return dfs(s)

学习一波新的操作

class Solution(object):
    def isSubtree(self, s, t):
        """
        :type s: TreeNode
        :type t: TreeNode
        :rtype: bool
        """
        def convert(p):
            return "^" + str(p.val) + "#" + convert(p.left) + convert(p.right) if p else "$"

        return convert(t) in convert(s)

将树均转换为模式串,然后利用String中的知识来进行求解问题。真的是新的思路呀。
因为是要子串,所以与子串完全匹配,所以,需要区分那个是叶子节点,也就是在叶子节点之后需要加标识符,但因为节点之间的数值,也需要区分数值,需要前后分隔。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值