-
Notifications
You must be signed in to change notification settings - Fork 0
/
树的子结构.cpp
42 lines (37 loc) · 1.1 KB
/
树的子结构.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
/* *************************
* 树的子结构(代码鲁棒性)
* 输入两颗二叉树A,B,判断B是不是A的子结构。
* *************************/
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
class Solution {
public:
bool DoesTree1HasTree2(TreeNode* pRoot1, TreeNode* pRoot2){
if (pRoot2==NULL)
return true;
if (pRoot1==NULL)
return false;
if(pRoot1->val != pRoot2->val)
return false;
return DoesTree1HasTree2(pRoot1->left, pRoot2->left) && DoesTree1HasTree2(pRoot1->right, pRoot2->right);
}
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
bool result = false;
if(pRoot1!=NULL && pRoot2!=NULL){
if(pRoot1->val == pRoot2->val)
result = DoesTree1HasTree2(pRoot1, pRoot2);
if(!result)
result = HasSubtree(pRoot1->left, pRoot2);
if(!result)
result = HasSubtree(pRoot1->right, pRoot2);
}
return result;
}
};