subTree

struct Tree()
{	
	int val;
	Tree *left, *right;
	Tree(int a): val(a), left(NULL), right(NULL){}
}
bool hasSubTree(Tree *root1, Tree * root2){

	if(root2 == NULL) return true;
	if(root1 == NULL) return false;
	
	bool result = false;
	
	if(root1->val == root2->val)
		result = isSubTree(root1, root2);
	
	if(!result)
		result = hasSubTree(root1->left, root2);
	if(!result)
		result = hasSubTree(root1->right, root2);
		
	return result;
}

bool isSubTree(Tree *root1, Tree * root2){
	if(root2 == NULL) return true;
	if(root1 == NULL) return false;
	if(root1->val != root1->val) return false;
	return isSubTree(root1->left, root2->left)
	        && isSubTree(root1->right, root2->right); 
}

  

posted @ 2013-09-14 17:18  冰点猎手  阅读(300)  评论(0编辑  收藏  举报