-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path572.cpp
More file actions
25 lines (23 loc) · 757 Bytes
/
Copy path572.cpp
File metadata and controls
25 lines (23 loc) · 757 Bytes
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
/**
* 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 && !t) return true;
else if(!s && t || s && !t) return false;
return same(s, t) || isSubtree(s->left, t) || isSubtree(s->right, t);
}
bool same(TreeNode* s, TreeNode* t){
if(!s && !t) return true;
else if((s && !t) || (!s && t) || s->val != t->val) return false;
else if(!s->left && !s->right && !t->left && !t->right && s->val==t->val) return true;
return same(s->left, t->left)&&same(s->right, t->right);
}
};