-
Notifications
You must be signed in to change notification settings - Fork 0
/
offer_26
34 lines (30 loc) · 884 Bytes
/
offer_26
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
//面试题26:树的子结构
bool HasSubTree(BinaryTreeNode* pRoot1,BinaryTreeNode* pRoot2)
{
bool result=false;
if(pRoot1!=nullptr&&pRoot2!=nullptr)
{
if(Equal(pRoot1->m_dbValue,pRoot->m_dbValue))
result=DosTree1HaveTree2(pRoot1,pRoot2);
if(!result)
result=HasSubtree(pRoot1->m_pLeft,pRoot2);
if(!result)
result=HasSubtree(pRoot1->m_pRight,pRoot2);
}
return result;
}
bool DoesTree1HaveTree(BinaryTreeNode* pRoot1,BinaryTreeNode* pRoot2)
{
if(pRoot2==nullptr) return false;
if(pRoot1==nullptr) return false;
if(!Equal(pRoot1->m_dbValue,pRoot2->m_dbValue))
return false;
return DoesTree1HaveTree(pRoot1->m_pLeft,pRoot2->m_pLeft)&&DoesTree1HaveTree(pRoot1->m_pRight,pRoot2->m_pRight);
}
bool Equal(double num1,double num2)
{
if((num1-num2>-0.0000001) && (num1-num2<0.0000001))
return true;
else
return false;
}