【树】leetcode_572_另一个树的子树
class Solution {
public:
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if (root == nullptr && subRoot == nullptr) return true;
if (root == nullptr || subRoot == nullptr) return false;
bool s = is_same(root, subRoot);
bool sl = isSubtree(root->left,subRoot);
bool sr = isSubtree(root->right, subRoot);
return s||sl||sr;
}
bool is_same(TreeNode *cur, TreeNode* pre){
if(cur == nullptr && pre == nullptr) return true;
if (cur==nullptr || pre == nullptr) return false;
if (cur->val != pre->val) return false;
bool ll = is_same(cur->left,pre->left);
bool rr = is_same(cur->right, pre->right);
return ll&&rr;
}
};