Skip to content

Commit

Permalink
Time: 30 ms (35.53%), Space: 28.9 MB (54.19%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
Dipendra-Raghav committed May 11, 2023
1 parent 13d5039 commit d683b62
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 0572-subtree-of-another-tree/0572-subtree-of-another-tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public:
bool dfs(TreeNode*root,TreeNode*target)
{
if(root==NULL && target==NULL)
return true;
if(root==NULL|| target==NULL)
return false;
if(root->val!=target->val)
return false;
return dfs(root->left,target->left) && dfs(root->right,target->right);

}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if(!root)
return false;
if(dfs(root, subRoot))
return true;
return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
}
};

0 comments on commit d683b62

Please sign in to comment.