-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubtree.cc
More file actions
64 lines (52 loc) · 1.31 KB
/
Copy pathSubtree.cc
File metadata and controls
64 lines (52 loc) · 1.31 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
59
60
61
62
63
64
#include <iostream>
using namespace std;
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2) {
if (pRoot1 == NULL || pRoot2 == NULL) return false;
if(sameTree(pRoot1, pRoot2)) return true;
bool left = false;
bool right = false;
if (pRoot1->left != NULL)
left = HasSubtree(pRoot1->left, pRoot2);
if (pRoot1->right != NULL)
right = HasSubtree(pRoot1->right, pRoot2);
return left || right;
}
bool sameTree(TreeNode* a, TreeNode* b) {
if (a == NULL && b == NULL) return true;
if (a == NULL && b != NULL ||
a != NULL && b == NULL) return false;
if (a->val == b->val)
return sameTree(a->left, b->left) && sameTree(a->right, b->right);
return false;
}
};
int main(int argc, char **argv) {
TreeNode n1(1);
TreeNode n2(2);
TreeNode n3(3);
TreeNode n4(4);
TreeNode n5(5);
TreeNode n6(6);
n1.left = &n2;
n1.right = &n3;
n2.left = &n4;
n2.right = &n5;
n5.right = &n6;
TreeNode s1(2);
TreeNode s2(4);
TreeNode s3(5);
TreeNode s4(6);
s1.left = &s2;
s1.right = &s3;
Solution s;
cout << s.HasSubtree(&n1, &s1) << endl;
}