1. 程式人生 > >leetcode 572. Subtree of Another Tree

leetcode 572. Subtree of Another Tree

時間復雜度 www. example 子節點 use 完整 str hash表 truct

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:

     3
    /    4   5
  /  1   2

Given tree t:

   4 
  /  1   2

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

Example 2:
Given tree s:

     3
    /    4   5
  /  1   2
    /
   0

Given tree t:

   4
  /  1   2

Return false.

解法1:

# 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 isSubtree(self, s, t):
        """
        :type s: TreeNode
        :type t: TreeNode
        :rtype: bool
        """
        def same(s, t):
            if s is None or t is None:
                return s == t
            else:
                return s.val == t.val and same(s.left, t.left) and same(s.right, t.right)
        if s is None:
            return t is None
        return same(s, t) or self.isSubtree(s.left, t) or self.isSubtree(s.right, t)

解法2:

先遍歷樹,生成字符串,然後利用字符串查找思路做。不喜歡這種解法,因為字符串查找時間復雜度最壞也是O(nm)。

解法3:

類似simhash或者Merkle 樹——空間換時間,分而治之的hash表,通過根節點是由它的兩個子節點內容的哈希值組成來校驗數據完整性,定位篡改的數據位置算法,每一個節點自下而上計算其hash,然後比較hash是否相等即可判定是否有相同子樹。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    boolean hasSub = false;
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if (s == null) return t == null;
        hashAndCompare(s, t, hashAndCompare(t, null, 0));
        return hasSub;
    }
    
    private int hashAndCompare(TreeNode s, TreeNode t, int tHash) {
        if (s == null) return 0;
        int myHash = hashAndCompare(s.left, t, tHash) + hashAndCompare(s.right, t, tHash) + (s.val == 0? 1337 : s.val) * 401;
        if (myHash == tHash && !hasSub) hasSub = equals(s, t);
        return myHash;
    }
    
    private boolean equals(TreeNode s, TreeNode t) {
        if (s == null || t == null) return s == t;
        if (s.val != t.val) return false;
        return equals(s.left, t.left) && equals(s.right, t.right);
    }
}

我自己的解法:

# 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 isSubtree(self, s, t):
        """
        :type s: TreeNode
        :type t: TreeNode
        :rtype: bool
        """        
        ans = False
        
        def same_tree(t1, t2):
            if t1 is None or t2 is None:
                return t1 == t2
            return t1.val == t2.val and same_tree(t1.left, t2.left) and same_tree(t1.right, t2.right)
            
        def hash_node(node, hash_t):
            if not node:                
                return 0
            l = hash_node(node.left, hash_t)            
            r = hash_node(node.right, hash_t)
            h = hash(str(l+r+node.val))
            nonlocal ans
            if h == hash_t and not ans:                
                ans = same_tree(node, t) 
            return h
        
        hash_t = hash_node(t, None)    
        hash_node(s, hash_t)
        return ans
    

leetcode 572. Subtree of Another Tree