链接:http://www.nowcoder.com/practice/6e196c44c7004d15b1610b9afca8bd88?tpId=13&tqId=11170&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) { if(pRoot2==NULL) return false; return solve(pRoot1,pRoot2,pRoot2->val); } bool solve(TreeNode* p1,TreeNode* p2,int val) { if(p1==NULL) return false; if(p1->val==val&&same(p1,p2)) return true; return solve(p1->left,p2,val)||solve(p1->right,p2,val); } bool same(TreeNode*p,TreeNode* pp) { if(pp==NULL) return true; if(p==NULL) return false; if(p->val!=pp->val) return false; return same(p->left,pp->left)&&same(p->right,pp->right); } };