描述
控制台
245. 子树
有两个不同大小的二叉树: T1
有上百万的节点; T2
有好几百的节点。请设计一种算法,判定 T2
是否为 T1
的子树。
样例
下面的例子中 T2 是 T1 的子树:
1 3
/ \ /
T1 = 2 3 T2 = 4
/
4
下面的例子中 T2 不是 T1 的子树:
1 3
/ \ \
T1 = 2 3 T2 = 4
/
4
注意事项
若 T1 中存在从节点 n 开始的子树与 T2 相同,我们称 T2 是 T1 的子树。也就是说,如果在 T1 节点 n 处将树砍断,砍断的部分将与 T2 完全相同。
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param T1: The roots of binary tree T1.
* @param T2: The roots of binary tree T2.
* @return: True if T2 is a subtree of T1, or false.
*/
bool isSubtree(TreeNode * T1, TreeNode * T2) {
// write your code here
if(T1==NULL && T2==NULL) return true;
if(T1==NULL && T2!=NULL) return false;
if(T1!=NULL && T2==NULL) return true;
string res1=Seriers(T1);
string res2=Seriers(T2);
int R=res2.size();
int pos=0;
for(int i=0;i<res1.size();)
{
if(res1[i]!=res2[pos])
{
int k=res2.size()-1;
while(k>0 && R<res2.size() && res2[k]!=res1[R])
{
k--;
}
i=R-k;
pos=0;
R=i+res2.size();
}else
{
if(pos==res2.size()-1)
{
return true;
}
pos++;
i++;
}
}
return false;
}
private:
string Seriers(TreeNode* TREE)
{
if(TREE==NULL) return "#!";
string res=to_string(TREE->val)+'!';
res+=Seriers(TREE->left);
res+=Seriers(TREE->right);
return res;
}
};
######################################################
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param T1: The roots of binary tree T1.
* @param T2: The roots of binary tree T2.
* @return: True if T2 is a subtree of T1, or false.
*/
public boolean isSubtree(TreeNode T1, TreeNode T2) {
// write your code here
if(T1==null && T2==null) return true;
if(T1!=null && T2==null) return true;
if(T1==null && T2!=null) return false;
char[] str=Seriers(T1).toCharArray();
char[] match=Seriers(T2).toCharArray();
int pos=0;
int R=match.length;
for (int i=0;i<str.length ; )
{
if(str[i]!=match[pos])
{
int k=match.length -1;
while(k>0 && R<str.length && str[R]!=match[k])
{
k--;
}
i=R-k;
pos=0;
R=i+match.length;
}else
{
if(pos==match.length-1)
{
return true;
}
pos++;
i++;
}
}
return false;
}
private String Seriers(TreeNode tree)
{
if(tree==null) return "#!";
String res=String.valueOf(tree.val)+'!';
res+=Seriers(tree.left);
res+=Seriers(tree.right);
return res;
}
}
###############################################################
"""
Definition of TreeNode:
class TreeNode:
def __init__(self, val):
self.val = val
self.left, self.right = None, None
"""
class Solution:
"""
@param T1: The roots of binary tree T1.
@param T2: The roots of binary tree T2.
@return: True if T2 is a subtree of T1, or false.
"""
def isSubtree(self, T1, T2):
# write your code here
if T1 == None and T2 == None:
return True
if T1 == None and T2 != None:
return False
if T1 != None and T2 == None:
return True
strr = []
match = []
self.Seriers(T1, strr)
self.Seriers(T2, match)
pos=0
index=0
R=len(match)
while index<len(strr):
if strr[index]!=match[pos]:
k=len(match)-1
while k>0 and R<len(match) and match[k]!=strr[R]:
k-=1
index=R-k
pos=0
R=index+len(match)
else:
if pos==len(match)-1:
return True
index+=1
pos+=1
return False
def Seriers(self,Tree,strings):
if Tree==None:
return strings.append('#!')
strings.append(str(Tree.val)+'!')
self.Seriers(Tree.left,strings)
self.Seriers(Tree.right,strings)