/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool issameroot(struct TreeNode* p,struct TreeNode* q){
if(!p&&!q) return true;
if(!q&&p || q&&!p) return false;
if(q->val!=p->val) return false;
return issameroot(p->left,q->left)&&issameroot(q->right,p->right);
}
bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){
if(!root&&!subRoot) return true;
if(!root&&subRoot ||root&&!subRoot ) return false;
bool a=isSubtree(root->left,subRoot);
bool b=isSubtree(root->right,subRoot);
bool c=issameroot(root,subRoot);
return a||b||c;
}
我是先后序遍历,然后从低往上把每个子树都对比一下。
标签:子树,TreeNode,struct,572,一棵树,subRoot,&&,return,root From: https://www.cnblogs.com/llllmz/p/18054241