树的子结构判断

题目:输入两棵二叉树A和B,判断B是不是A的子结构。

 1 #include<iostream>
 2 using namespace std;
 3 
 4 struct BinaryTreeNode {
 5     int m_nValue;
 6     BinaryTreeNode* m_pLeft;
 7     BinaryTreeNode* m_pRight;
 8 }
 9 
10 bool DoesTree1HasTree2(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2) {
11     if(pRoot2 == NULL)
12         return true;
13     if(pRoot1 == NULL)
14         return false;
15 
16     if(pRoot2->m_nValue == pRoot1->m_nValue)
17         return false;
18     return DoesTree1HasTree2(pRoot1->m_pLeft, pRoot2->m_pLeft) 
19         && DoesTree1HasTree2(pRoot1->m_pRight, pRoot2->m_pRight);
20 }
21 
22 bool HasSubTree(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2) {
23     bool result = false;
24 
25     if(pRoot1 != NULL && pRoot2 != NULL) {
26         if(pRoot2->m_nValue == pRoot1->m_nValue)
27             result = DoesTree1HasTree2(pRoot1, pRoot2);
28         if(!result)
29             result = HasSubTree(pRoot1->m_pLeft, pRoot2);
30         if(!result)
31             result = HasSubTree(pRoot1->m_pRight, pRoot21);
32     }
33 }
34 
35 int main()
36 {
37     
38     return 0;
39 }

 

转载于:https://www.cnblogs.com/wanghui390/p/3772954.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值