forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubtree.cpp
126 lines (114 loc) · 3.47 KB
/
subtree.cpp
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Time: O(m * n), n is number of the nodes in larger tree, m is number of the nodes in smaller one.
// Space: O(1)
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
// Morris Traversal (Preorder) if the data of the tree nodes could be modified.
class Solution {
public:
/**
* @param T1, T2: The roots of binary tree.
* @return: True if T2 is a subtree of T1, or false.
*/
bool isSubtree(TreeNode *T1, TreeNode *T2) {
if (!T2) {
return true;
}
bool found = false;
TreeNode *curr = T1;
while (curr) {
if (!curr->left) {
if (!found && isSameTree(curr, curr, T2)) {
found = true;
}
curr = curr->right;
} else {
TreeNode *node = curr->left;
while (node->right && node->right != curr) {
node = node->right;
}
// Traverse the left child if only if the subtree is not found.
if (!found && !node->right) {
if (isSameTree(curr, curr, T2)) {
found = true;
}
node->right = curr;
curr = curr->left;
} else {
// Rollback the modification.
node->right = nullptr;
curr = curr->right;
}
}
}
// Complete the traversal to rollback the modification due to Morris Traversal.
return found;
}
bool isSameTree(const TreeNode *T1_root,
const TreeNode *T1, const TreeNode *T2) {
if (!T1 && !T2) {
return true;
}
if (T1 && T2) {
return T1->val == T2->val &&
isSameTree(T1_root, T1->left, T2->left) &&
isSameTree(T1_root, realRightChild(T1), T2->right);
}
return false;
}
// Treat the right child as nullptr if it has been used for
// Morris Traversal.
TreeNode *realRightChild(const TreeNode *curr) {
TreeNode* curr_right = curr ? curr->right : nullptr;
if (curr_right && curr_right->left) {
TreeNode *node = curr_right->left;
while (node->right && node->right != curr_right) {
node = node->right;
}
if (node->right) {
curr_right = nullptr;
}
}
return curr_right;
}
};
// Time: O(m * n)
// Space: O(h)
class Solution2 {
public:
/**
* @param T1, T2: The roots of binary tree.
* @return: True if T2 is a subtree of T1, or false.
*/
bool isSubtree(TreeNode *T1, TreeNode *T2) {
if (!T2) {
return true;
} else if (!T1) { // !T1 && T2
return false;
} else { // T1 && T2
return isSameTree(T1, T2) ||
isSubtree(T1->left, T2) ||
isSubtree(T1->right, T2);
}
}
bool isSameTree(TreeNode *T1, TreeNode *T2) {
if (!T1 && !T2) {
return true;
}
if (T1 && T2) {
return T1->val == T2->val &&
isSameTree(T1->left, T2->left) &&
isSameTree(T1->right, T2->right);
}
return false;
}
};