1
/**
2
* Definition for a binary tree node.
3
* struct TreeNode {
4
* int val;
5
* TreeNode *left;
6
* TreeNode *right;
7
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left),
10
* right(right) {}
11
* };
12
*/
13
class Solution {
14
public:
15
void sol(TreeNode *root, string &s) {
16
queue<TreeNode *> q;
17
q.push(root);
18
s += to_string(root->val);
19
while (!q.empty()) {
20
auto x = q.front();
21
q.pop();
22
if (x->left) {
23
s += to_string(x->left->val);
24
q.push(x->left);
25
} else if (!x->left) {
26
s += '#';
27
}
28
if (x->right) {
29
s += to_string(x->right->val);
30
q.push(x->right);
31
} else if (!x->right) {
32
s += '#';
33
}
34
}
35
}
36
bool isSubtree(TreeNode *root, TreeNode *subRoot) {
37
string s;
38
sol(subRoot, s);
39
cout << s << " ";
40
queue<TreeNode *> q;
41
q.push(root);
42
while (!q.empty()) {
43
auto x = q.front();
44
q.pop();
45
if (x->val == subRoot->val) {
46
string k;
47
sol(x, k);
48
cout << k << " ";
49
bool istrue = false;
50
if (k.size() == s.size()) {
51
for (int i = 0; i < k.size(); i++) {
52
if (k[i] != s[i]) {
53
istrue = true;
54
}
55
}
56
if (istrue == false) {
57
return true;
58
}
59
}
60
}
61
if (x->left) {
62
q.push(x->left);
63
}
64
if (x->right) {
65
q.push(x->right);
66
}
67
}
68
return false;
69
}
70
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0