面试题18 二叉树B是不是A的子结构

思路:首先在树A中找到和B的根结点的值一样的结点R,然后再判断树A中以R为根的子树是否包含树B一样的结构。

递归代码:

[cpp]  view plain copy
  1. #include "stdafx.h"  
  2. #include <iostream>  
  3. using namespace std;  
  4.   
  5. struct BinaryTreeNode  
  6. {  
  7.     int m_nValue;  
  8.     BinaryTreeNode *m_pLeft;  
  9.     BinaryTreeNode *m_pRight;  
  10. };  
  11.   
  12. bool DoesTree1haveTree2(BinaryTreeNode *pRoot1, BinaryTreeNode *pRoot2)  
  13. {  
  14.     if (pRoot2 == NULL)  
  15.     {  
  16.         return true;  
  17.     }  
  18.   
  19.     if (pRoot1 == NULL)  
  20.     {  
  21.         return false;  
  22.     }  
  23.   
  24.     if (pRoot1->m_nValue != pRoot2->m_nValue)  
  25.     {  
  26.         return false;  
  27.     }  
  28.   
  29.     return DoesTree1haveTree2(pRoot1->m_pLeft, pRoot2->m_pLeft)  
  30.         && DoesTree1haveTree2(pRoot1->m_pRight, pRoot2->m_pRight);  
  31. }  
  32.   
  33. //判断B是否为A的子结构  
  34. bool IsSubStruct(BinaryTreeNode *pRootOfTreeA, BinaryTreeNode *pRootOfTreeB)  
  35. {  
  36.    bool result = false;  
  37.    if (pRootOfTreeA != NULL && pRootOfTreeB != NULL)  
  38.    {  
  39.        if (pRootOfTreeA->m_nValue == pRootOfTreeB->m_nValue)  
  40.        {  
  41.            result = DoesTree1haveTree2(pRootOfTreeA, pRootOfTreeB);  
  42.        }  
  43.   
  44.        if (!result)  
  45.        {  
  46.            result = IsSubStruct(pRootOfTreeA->m_pLeft, pRootOfTreeB);  
  47.        }  
  48.   
  49.        if (!result)  
  50.        {  
  51.            result = IsSubStruct(pRootOfTreeA->m_pRight, pRootOfTreeB);  
  52.        }  
  53.    }   
  54.   
  55.    return result;  
  56. }  
  57.   
  58. //以先序的方式构建二叉树,输入-1表示结点为空  
  59. void CreateBinaryTree(BinaryTreeNode *&pRoot)  
  60. {  
  61.     int nNodeValue = 0;  
  62.     cin >> nNodeValue;      
  63.     if (-1 == nNodeValue)  
  64.     {  
  65.        return;   
  66.     }  
  67.     else  
  68.     {  
  69.         pRoot = new BinaryTreeNode();  
  70.         pRoot->m_nValue = nNodeValue;  
  71.         CreateBinaryTree(pRoot->m_pLeft);  
  72.         CreateBinaryTree(pRoot->m_pRight);  
  73.     }  
  74. }  
  75.   
  76. void PrintInOrder(BinaryTreeNode *&pRoot)  
  77. {  
  78.     if (pRoot != NULL)  
  79.     {  
  80.         PrintInOrder(pRoot->m_pLeft);  
  81.         cout << pRoot->m_nValue << " ";  
  82.         PrintInOrder(pRoot->m_pRight);  
  83.     }  
  84. }  
  85.   
  86. int _tmain(int argc, _TCHAR* argv[])  
  87. {  
  88.     BinaryTreeNode *pRoot1 = NULL;  
  89.     CreateBinaryTree(pRoot1);  
  90.     PrintInOrder(pRoot1);  
  91.     cout << endl;  
  92.   
  93.     BinaryTreeNode *pRoot2 = NULL;  
  94.     CreateBinaryTree(pRoot2);  
  95.     PrintInOrder(pRoot2);  
  96.     cout << endl;  
  97.   
  98.     cout << "Root2是否为Root1的子结构,是返回1,否则返回0.结果是: " << IsSubStruct(pRoot1, pRoot2) << endl;  
  99.   
  100.     system("pause");  
  101.     return 0;  
  102. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值