-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
26 additions
and
19 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
"conio.h": "c", | ||
"array": "cpp", | ||
"string": "cpp", | ||
"string_view": "cpp" | ||
"string_view": "cpp", | ||
"iostream": "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
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
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
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 |
---|---|---|
@@ -1,30 +1,33 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
struct Tree | ||
struct TreeNode | ||
{ | ||
int val; | ||
Tree *left; | ||
Tree *right; | ||
TreeNode *left; | ||
TreeNode *right; | ||
}; | ||
|
||
bool isIdentical(Tree *root1, Tree *root2) | ||
bool areIdentical(TreeNode *root, TreeNode *subRoot) | ||
{ | ||
if (root1 == nullptr && root2 == nullptr) | ||
if (root == nullptr && subRoot == nullptr) | ||
return true; | ||
if (root1 == nullptr || root2 == nullptr) | ||
if (root == nullptr || subRoot == nullptr) | ||
return false; | ||
return root1->val == root2->val && isIdentical(root1->left, root2->left) && | ||
isIdentical(root1->right, root2->right); | ||
if (root->val == subRoot->val && areIdentical(root->left, subRoot->left) && areIdentical(root->right, subRoot->right)) | ||
{ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool solve(Tree *root, Tree *target) | ||
bool isSubtree(TreeNode *root, TreeNode *subRoot) | ||
{ | ||
if (target == nullptr) | ||
return true; | ||
if (root == nullptr) | ||
return false; | ||
if (isIdentical(root, target)) | ||
if (subRoot == nullptr) | ||
return true; | ||
if (areIdentical(root, subRoot)) | ||
return true; | ||
return solve(root->left, target) || solve(root->right, target); | ||
return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot); | ||
} |