[Leetcode] Binary tree-- 572. Subtree of Another Tree

Posted 安新

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Leetcode] Binary tree-- 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:

     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.

 

Solution:

  1.method use recursive and compare whether it is the same tree for each substree rooted at s\' and the tree t
      it takes o(mn) time ; m is the size of s, n is the size of t
 
 2. use preorder traversal and innorder traversal;  o(m+n)
 
Here I analysis why for both traversal.
 for inorder traversal
  for example 1:  s list [ #, 1, #, 4, #, 2,  #,3,  #, 5, #] ,  t list [#, 1, #, 4, #, 2, #]
      example 2: s list [#, 1,#, 4, #, 0, #, 2, #, 3, 5, #, #], t list [ #, 1, #, 4, #, 2, #]
      example 3:  s list [ #, 4, #, 3, #, 5, #, 1, #, 2, #];       [#, 4, #, 3, #];           it is the sublist of s, but not the subtree of s,  does not work for inorder
      but for the preorder of this, s list [1, 4, #, 3, #, 5, #, #, 2, #, #], t list [4, #,3, #, #] ;       it works.
 
 1         def helperRecursiveInOrder(root, l):
 2             if root is None:
 3                 l.append (str("#"))
 4             else:
 5                 helperRecursiveInOrder(root.left, l)
 6                 l.append(str(root.val))
 7                 helperRecursiveInOrder(root.right, l)
 8         
 9                 
10         def helperRecursivePreOrder(root, l):
11             if root is None:
12                 l.append (str("#"))
13             else:
14                 l.append(str(root.val))
15                 helperRecursivePreOrder(root.left, l)
16                 helperRecursivePreOrder(root.right, l)
17         
18         
19         lsIn = []
20         helperRecursiveInOrder(s, lsIn)
21         ltIn = []
22         helperRecursiveInOrder(t, ltIn)
23         strSIn = ",".join(lsIn)
24         strTIn = ",".join(ltIn)
25         
26         lsPre = []
27         helperRecursivePreOrder(s, lsPre)
28         ltPre = []
29         helperRecursivePreOrder(t, ltPre)
30         strSPre = ",".join(lsPre)
31         strTPre = ",".join(ltPre)
32         #print ("str: ", strSPre, strTPre, lsPre)
33         if(strTIn in strSIn and strTPre in strSPre):                     
34             return True
35         else:
36             return False
 
3. After finishing this, I checked the answer online, actually, it used preorder traversal  only to tackle this problem.
 if we simply process the case like s = [12] , t =[2];     with the \',\' added to each node,
 with the preOrder, it also works.
 
 1         def helperRecursivePreOrder(root, l):
 2             if root is None:
 3                 l.append (str("#"))
 4             else:
 5                 l.append(\',\' + str(root.val))
 6                 helperRecursivePreOrder(root.left, l)
 7                 helperRecursivePreOrder(root.right, l)
 8         lsPre = []
 9         helperRecursivePreOrder(s, lsPre)
10         ltPre = []
11         helperRecursivePreOrder(t, ltPre)
12         strSPre = ",".join(lsPre)
13         strTPre = ",".join(ltPre)
14         #print ("str: ", strSPre, strTPre, lsPre)
15         if(strTPre in strSPre):                     
16             return True
17         else:
18             return False
19         

 

以上是关于[Leetcode] Binary tree-- 572. Subtree of Another Tree的主要内容,如果未能解决你的问题,请参考以下文章

[Leetcode] Binary tree-- 606. Construct String from Binary Tree

[Leetcode] Binary search tree --Binary Search Tree Iterator

Leetcode[110]-Balanced Binary Tree

[Leetcode] Binary tree -- 501. Find Mode in Binary Search Tree

[Lintcode]95. Validate Binary Search Tree/[Leetcode]98. Validate Binary Search Tree

LeetCode 110. Balanced Binary Tree 递归求解