和剑指上树的子结构的题稍稍有点不同,
3 / 4 5 / 1 2 / 0
4 / 1 2
这两个在这个题目中不是子树的关系,但剑指那个题这种情况算是子树
class Solution { public: bool isSubtree(TreeNode* s, TreeNode* t) { bool result = false; if(s != NULL && t != NULL){ if(s->val == t->val) result = isSubCore(s,t); if(!result) result = isSubtree(s->left,t); if(!result) result = isSubtree(s->right,t); } return result; } bool isSubCore(TreeNode* s,TreeNode* t){ if(t == NULL && s == NULL) return true; else if(t == NULL && s != NULL) return false; else if(t != NULL && s == NULL) return false; if(s->val != t->val) return false; return isSubCore(s->left,t->left) && isSubCore(s->right,t->right); } };