[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.

Refer to the work from  http://www.cnblogs.com/grandyang/p/6828687.html

 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         
时间: 2024-12-17 04:53:02

[Leetcode] Binary tree-- 572. Subtree of Another Tree的相关文章

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 considere

【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 con

LeetCode 572: Subtree of Another Tree

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public boolean isSubtree(TreeNode s, TreeNode t) { if (s == null) { return false;

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 considere

[LC] 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 considere

【easy】572. Subtree of Another Tree

判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二:用递归来写十分的简洁,我们先从s的根结点开始,跟t比较,如果两棵树完全相同,那么返回true,否则就分别对s的左子结点和右子结点调用递归再次来判断是否相同,只要有一个返回true了,就表示可以找得到. class Solution { public: bool isSubtree(TreeNode

LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40

572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验?s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 每日一算法2019/6/12Day 40LeetCode572. Subtree of Another Tree 示例 1: 给定的树 s: 3 / 4 5 / 1 2 给定的树 t: 4 / 1 2 返回 true,因为 t

[Leetcode] 1120. Maximum Average Subtree

Given the root of a binary tree, find the maximum average value of any subtree of that tree. (A subtree of a tree is any node of that tree plus all its descendants. The average value of a tree is the sum of its values, divided by the number of nodes.

[LeetCode] Binary Tree Tilt 二叉树的坡度

Given a binary tree, return the tilt of the whole tree. The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all right subtree node values. Null node has tilt 0. The tilt of the 

[leetcode]Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#