Skip to content

Commit 145428a

Browse files
committed
Subtree of Another solution
1 parent 688bc99 commit 145428a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

subtree-of-another-tree/PDKhan.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
bool compare_tree(TreeNode* root, TreeNode* subRoot) {
4+
if(!root && !subRoot)
5+
return true;
6+
else if(!root || !subRoot || root->val != subRoot->val)
7+
return false;
8+
9+
return compare_tree(root->left, subRoot->left) && compare_tree(root->right, subRoot->right);
10+
}
11+
12+
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
13+
if(!root)
14+
return false;
15+
16+
if(compare_tree(root, subRoot))
17+
return true;
18+
19+
return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
20+
}
21+
};

0 commit comments

Comments
 (0)