-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Time: 30 ms (35.53%), Space: 28.9 MB (54.19%) - LeetHub
- Loading branch information
1 parent
13d5039
commit d683b62
Showing
1 changed file
with
21 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
0572-subtree-of-another-tree/0572-subtree-of-another-tree.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |