We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 688bc99 commit 145428aCopy full SHA for 145428a
subtree-of-another-tree/PDKhan.cpp
@@ -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
15
16
+ if(compare_tree(root, subRoot))
17
18
19
+ return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
20
21
+};
0 commit comments