-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0572_subtree_of_another_tree.cpp
More file actions
34 lines (30 loc) · 1.41 KB
/
0572_subtree_of_another_tree.cpp
File metadata and controls
34 lines (30 loc) · 1.41 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
// https://leetcode.com/problems/subtree-of-another-tree/
// O(n * m) time complexity where n is the size of root tree and m is the size of the subtree. For each node in the tree, we're checking the created subtree with the given subtree.
// O(max(n, m)) space complexity. It'd be O(n+m) for 2 highly skewed trees, but the calls are not simultaneous and the isSubtree recursion resumes only after isSameTree completes, the actual maximum call stack at any point is O(max(n, m)).
/**
* 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) {}
* };
*/
class Solution {
public:
bool isSubtree(TreeNode* root, TreeNode* subRoot) {
if (!root) return false;
if (isSameTree(root, subRoot)) {
return true;
}
return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
}
private:
bool isSameTree(TreeNode* pNode, TreeNode* qNode) {
if (pNode == nullptr && qNode == nullptr) return true;
if (pNode == nullptr || qNode == nullptr) return false;
return (pNode->val == qNode->val) && isSameTree(pNode->left, qNode->left) && isSameTree(pNode->right, qNode->right);
}
};