Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- bool isSubtree(TreeNode* s, TreeNode* t) {
- if(!s) return false;
- if(isSame(s,t)) return true;
- return isSubtree(s->left,t) || isSubtree(s->right,t);
- }
- bool isSame(TreeNode *s, TreeNode *t){
- if (!s && !t) return true;
- if (!s || !t) return false;
- if (s->val != t->val) return false;
- return isSame(s->left, t->left) && isSame(s->right, t->right);
- }
- };
- -------------------
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- bool isSubtree(TreeNode* root, TreeNode* subRoot) {
- string text = serialize(root);
- string pat = serialize(subRoot);
- return text.find(pat) != string::npos;
- }
- std::hash<std::string> str_hash;
- string serialize(TreeNode *root){
- if(!root) return "#";
- return to_string(str_hash(to_string(root->val)))+","+serialize(root->left)+","+serialize(root->right);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement