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