Skip to content

Instantly share code, notes, and snippets.

@Ray1988
Last active December 15, 2015 00:09
Show Gist options
  • Save Ray1988/5171157 to your computer and use it in GitHub Desktop.
Save Ray1988/5171157 to your computer and use it in GitHub Desktop.
decide if T2 is subTree of T1
public boolean contain(TreeNode t1, TreeNode t2){
if(t2==null) return true// null tree is alway a subtree
return subTree(t1,t2)
}
public boolean subTree(TreeNode t1, TreeNode t2){
if(t1==null) return false// big tree is null, t2 must not be a subtree
if (t1.data=t2.data){
if(match(t1, t2)) return true
}
return subTree(t1.left, t2)||(t1.right, t2)
}
public boolean match(TreeNode t1, TreeNdoe t2){
if(t1==null&&t2==null) return true
if(t1==null||t2==null) return false
if(t1.data!=t1.data) return false
return matchTree(t1.left, t2.left)&&(t1.right, t2.right)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment