-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubtree_of_another_tree.cpp
More file actions
58 lines (48 loc) · 1.75 KB
/
subtree_of_another_tree.cpp
File metadata and controls
58 lines (48 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// 572. Subtree of Another Tree: https://leetcode.com/problems/subtree-of-another-tree/
// tree, dfs, traversal, recursion, easy
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
/*
approach:
three case:
case 1: if root value and subroot values are null then return true
case 2: if both values are same then go for both left and right subtree for both trees are same, if then return true
case 3: if one subcase for left and right not equal for case 2 then return false, also for other case return false
*/
class Solution {
public:
bool ans = false;
bool match(TreeNode* root, TreeNode* subRoot){
if(root != NULL && subRoot != NULL){
bool a = match(root->left, subRoot->left);
bool b = match(root->right, subRoot->right);
if(root->val == subRoot->val && a && b) return true;
else return false;
}
else if(root == NULL && subRoot == NULL) return true;
else return false;
}
void inorder(TreeNode* root, TreeNode* subRoot){
if(root!=NULL){
inorder(root->left, subRoot);
bool x = match(root, subRoot);
if(x){ans = x;}
inorder(root->right, subRoot);
}
}
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
inorder(root, subRoot);
return ans;
}
};
// TC: O(n) as for the recursion we are traversing the whole tree
// SC: O(1) we are not storing anything extra, so should be O(1)