-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path572.txt
29 lines (27 loc) · 814 Bytes
/
572.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 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 isSubtree(TreeNode* s, TreeNode* t) {
if (s == nullptr) return false;
if (isSame(s,t)) return true;
else return isSubtree(s->left,t) || isSubtree(s->right,t);
}
private:
bool isSame(TreeNode* s, TreeNode* t){
if(s == nullptr && t == nullptr) return true;
else if(s == nullptr || t == nullptr) return false;
if(s->val == t -> val){
if (isSame(s->left,t->left) && isSame(s->right,t->right)) return true;
else return false;
}
else return false;
}
};