题目:
分析:
就是不认真,小错误太多了吧!!!
刚开始忽略了B终止,而A还可以继续的情况。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool f2(TreeNode* A, TreeNode* B)
{
if(B==NULL) return 1;
if(A==NULL) return 0;
if(A->val!=B->val) return 0;
return f2(A->left,B->left) && f2(A->right,B->right);
}
bool f(TreeNode* A, TreeNode* B)
{//深搜找根
if(A==NULL) return 0;
if(B->val==A->val) if(f2(A,B)) return 1;
if(f(A->left,B)) return 1;
if(f(A->right,B)) return 1;
return 0;
}
bool isSubStructure(TreeNode* A, TreeNode* B) {
if(B==NULL) return 0;
return f(A,B);
}
};