1. #include"iostream"
  2. #include"stdio.h"
  3. #include"math.h"
  4. using namespace std;
  5.  
  6. struct BinaryTreeNode
  7. {
  8. double m_Value;
  9. BinaryTreeNode* m_pLeft;
  10. BinaryTreeNode* m_pRight;
  11. };
  12.  
  13. BinaryTreeNode* CreateBinaryTreeNode(double value)
  14. {
  15. BinaryTreeNode* pNode=new BinaryTreeNode();
  16. pNode->m_Value=value;
  17. pNode->m_pLeft=nullptr;
  18. pNode->m_pRight=nullptr;
  19.  
  20. return pNode;
  21. }
  22.  
  23. void ConnectTreeNodes(BinaryTreeNode* pParent,BinaryTreeNode* pLeft,BinaryTreeNode* pRight)
  24. {
  25. if(pParent!=nullptr)
  26. {
  27. pParent->m_pLeft=pLeft;
  28. pParent->m_pRight=pRight;
  29. }
  30. }
  31.  
  32. void PrintTreeNode(const BinaryTreeNode* pNode)
  33. {
  34. if(pNode!=nullptr)
  35. {
  36. cout<<"value of this node is:"<<pNode->m_Value<<endl;
  37.  
  38. if(pNode->m_pLeft!=nullptr)
  39. cout<<"value of its left child is:"<<pNode->m_pLeft->m_Value<<endl;
  40. else
  41. cout<<"left child is nullptr."<<endl;
  42. if(pNode->m_pRight!=nullptr)
  43. cout<<"value of its right child is:"<<pNode->m_pRight->m_Value<<endl;
  44. else
  45. cout<<"right child is nullptr."<<endl;
  46. }
  47. else
  48. cout<<"this node is nullptr."<<endl;
  49. cout<<endl;
  50. }
  51.  
  52. void PrintTree(const BinaryTreeNode* pRoot)
  53. {
  54. PrintTreeNode(pRoot);
  55.  
  56. if(pRoot!=nullptr)
  57. {
  58. if(pRoot->m_pLeft!=nullptr)
  59. PrintTreeNode(pRoot->m_pLeft);
  60.  
  61. if(pRoot->m_pRight!=nullptr)
  62. PrintTreeNode(pRoot->m_pRight);
  63. }
  64. }
  65.  
  66. void DestroyTree(BinaryTreeNode* pRoot)
  67. {
  68. if(pRoot!=nullptr)
  69. {
  70. BinaryTreeNode* pLeft=pRoot->m_pLeft;
  71. BinaryTreeNode* pRight=pRoot->m_pRight;
  72.  
  73. delete pRoot;
  74. pRoot=nullptr;
  75.  
  76. DestroyTree(pLeft);
  77. DestroyTree(pRight);
  78. }
  79. }
  80.  
  81. bool Equal(const double &a,const double &b)
  82. {
  83. if(fabs(a-b)<0.0000001)
  84. return true;
  85. return false;
  86. }
  87.  
  88. bool DoesTreeAHaveTreeB(BinaryTreeNode* pRootA,BinaryTreeNode* pRootB)
  89. {
  90. if(pRootB==nullptr)
  91. return true;
  92. if(pRootA==nullptr)
  93. return false;
  94.  
  95. if(Equal(pRootA->m_Value,pRootB->m_Value))
  96. {
  97. return DoesTreeAHaveTreeB(pRootA->m_pLeft,pRootB->m_pLeft)&&DoesTreeAHaveTreeB(pRootA->m_pRight,pRootB->m_pRight);
  98. }
  99. else
  100. {
  101. return false;
  102. }
  103. }
  104. bool HasSubTree(BinaryTreeNode* pRootA,BinaryTreeNode* pRootB)
  105. {
  106. if(pRootB==nullptr)
  107. return false;
  108. if(pRootA==nullptr)
  109. return false;
  110.  
  111. bool result=false;
  112.  
  113. if(Equal(pRootA->m_Value,pRootB->m_Value))
  114. {
  115. result=DoesTreeAHaveTreeB(pRootA,pRootB);
  116. }
  117. if(!result)
  118. {
  119. result=HasSubTree(pRootA->m_pLeft,pRootB);
  120. }
  121. if(!result)
  122. {
  123. result=HasSubTree(pRootA->m_pRight,pRootB);
  124. }
  125.  
  126. return result;
  127. }

函数

  1. #include"BinaryTree.h"
  2.  
  3. // ====================测试代码====================
  4. void Test(char* testName, BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2, bool expected)
  5. {
  6. if(HasSubTree(pRoot1, pRoot2) == expected)
  7. printf("%s passed.\n", testName);
  8. else
  9. printf("%s failed.\n", testName);
  10. }
  11.  
  12. // 树中结点含有分叉,树B是树A的子结构
  13. // 8 8
  14. // / \ / \
  15. // 8 7 9 2
  16. // / \
  17. // 9 2
  18. // / \
  19. // 4 7
  20. void Test1()
  21. {
  22. BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
  23. BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
  24. BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
  25. BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
  26. BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
  27. BinaryTreeNode* pNodeA6 = CreateBinaryTreeNode();
  28. BinaryTreeNode* pNodeA7 = CreateBinaryTreeNode();
  29.  
  30. ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
  31. ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
  32. ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);
  33.  
  34. BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
  35. BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
  36. BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
  37.  
  38. ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3);
  39.  
  40. Test("Test1", pNodeA1, pNodeB1, true);
  41.  
  42. DestroyTree(pNodeA1);
  43. DestroyTree(pNodeB1);
  44. }
  45.  
  46. // 树中结点含有分叉,树B不是树A的子结构
  47. // 8 8
  48. // / \ / \
  49. // 8 7 9 2
  50. // / \
  51. // 9 3
  52. // / \
  53. // 4 7
  54. void Test2()
  55. {
  56. BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
  57. BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
  58. BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
  59. BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
  60. BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
  61. BinaryTreeNode* pNodeA6 = CreateBinaryTreeNode();
  62. BinaryTreeNode* pNodeA7 = CreateBinaryTreeNode();
  63.  
  64. ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
  65. ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
  66. ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);
  67.  
  68. BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
  69. BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
  70. BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
  71.  
  72. ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3);
  73.  
  74. Test("Test2", pNodeA1, pNodeB1, false);
  75.  
  76. DestroyTree(pNodeA1);
  77. DestroyTree(pNodeB1);
  78. }
  79.  
  80. // 树中结点只有左子结点,树B是树A的子结构
  81. // 8 8
  82. // / /
  83. // 8 9
  84. // / /
  85. // 9 2
  86. // /
  87. // 2
  88. // /
  89. //
  90. void Test3()
  91. {
  92. BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
  93. BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
  94. BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
  95. BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
  96. BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
  97.  
  98. ConnectTreeNodes(pNodeA1, pNodeA2, nullptr);
  99. ConnectTreeNodes(pNodeA2, pNodeA3, nullptr);
  100. ConnectTreeNodes(pNodeA3, pNodeA4, nullptr);
  101. ConnectTreeNodes(pNodeA4, pNodeA5, nullptr);
  102.  
  103. BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
  104. BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
  105. BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
  106.  
  107. ConnectTreeNodes(pNodeB1, pNodeB2, nullptr);
  108. ConnectTreeNodes(pNodeB2, pNodeB3, nullptr);
  109.  
  110. Test("Test3", pNodeA1, pNodeB1, true);
  111.  
  112. DestroyTree(pNodeA1);
  113. DestroyTree(pNodeB1);
  114. }
  115.  
  116. // 树中结点只有左子结点,树B不是树A的子结构
  117. // 8 8
  118. // / /
  119. // 8 9
  120. // / /
  121. // 9 3
  122. // /
  123. // 2
  124. // /
  125. //
  126. void Test4()
  127. {
  128. BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
  129. BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
  130. BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
  131. BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
  132. BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
  133.  
  134. ConnectTreeNodes(pNodeA1, pNodeA2, nullptr);
  135. ConnectTreeNodes(pNodeA2, pNodeA3, nullptr);
  136. ConnectTreeNodes(pNodeA3, pNodeA4, nullptr);
  137. ConnectTreeNodes(pNodeA4, pNodeA5, nullptr);
  138.  
  139. BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
  140. BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
  141. BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
  142.  
  143. ConnectTreeNodes(pNodeB1, pNodeB2, nullptr);
  144. ConnectTreeNodes(pNodeB2, pNodeB3, nullptr);
  145.  
  146. Test("Test4", pNodeA1, pNodeB1, false);
  147.  
  148. DestroyTree(pNodeA1);
  149. DestroyTree(pNodeB1);
  150. }
  151.  
  152. // 树中结点只有右子结点,树B是树A的子结构
  153. // 8 8
  154. // \ \
  155. // 8 9
  156. // \ \
  157. // 9 2
  158. // \
  159. // 2
  160. // \
  161. // 5
  162. void Test5()
  163. {
  164. BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
  165. BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
  166. BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
  167. BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
  168. BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
  169.  
  170. ConnectTreeNodes(pNodeA1, nullptr, pNodeA2);
  171. ConnectTreeNodes(pNodeA2, nullptr, pNodeA3);
  172. ConnectTreeNodes(pNodeA3, nullptr, pNodeA4);
  173. ConnectTreeNodes(pNodeA4, nullptr, pNodeA5);
  174.  
  175. BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
  176. BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
  177. BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
  178.  
  179. ConnectTreeNodes(pNodeB1, nullptr, pNodeB2);
  180. ConnectTreeNodes(pNodeB2, nullptr, pNodeB3);
  181.  
  182. Test("Test5", pNodeA1, pNodeB1, true);
  183.  
  184. DestroyTree(pNodeA1);
  185. DestroyTree(pNodeB1);
  186. }
  187.  
  188. // 树A中结点只有右子结点,树B不是树A的子结构
  189. // 8 8
  190. // \ \
  191. // 8 9
  192. // \ / \
  193. // 9 3 2
  194. // \
  195. // 2
  196. // \
  197. // 5
  198. void Test6()
  199. {
  200. BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
  201. BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
  202. BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
  203. BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
  204. BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
  205.  
  206. ConnectTreeNodes(pNodeA1, nullptr, pNodeA2);
  207. ConnectTreeNodes(pNodeA2, nullptr, pNodeA3);
  208. ConnectTreeNodes(pNodeA3, nullptr, pNodeA4);
  209. ConnectTreeNodes(pNodeA4, nullptr, pNodeA5);
  210.  
  211. BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
  212. BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
  213. BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
  214. BinaryTreeNode* pNodeB4 = CreateBinaryTreeNode();
  215.  
  216. ConnectTreeNodes(pNodeB1, nullptr, pNodeB2);
  217. ConnectTreeNodes(pNodeB2, pNodeB3, pNodeB4);
  218.  
  219. Test("Test6", pNodeA1, pNodeB1, false);
  220.  
  221. DestroyTree(pNodeA1);
  222. DestroyTree(pNodeB1);
  223. }
  224.  
  225. // 树A为空树
  226. void Test7()
  227. {
  228. BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
  229. BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
  230. BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
  231. BinaryTreeNode* pNodeB4 = CreateBinaryTreeNode();
  232.  
  233. ConnectTreeNodes(pNodeB1, nullptr, pNodeB2);
  234. ConnectTreeNodes(pNodeB2, pNodeB3, pNodeB4);
  235.  
  236. Test("Test7", nullptr, pNodeB1, false);
  237.  
  238. DestroyTree(pNodeB1);
  239. }
  240.  
  241. // 树B为空树
  242. void Test8()
  243. {
  244. BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
  245. BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
  246. BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
  247. BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
  248.  
  249. ConnectTreeNodes(pNodeA1, nullptr, pNodeA2);
  250. ConnectTreeNodes(pNodeA2, pNodeA3, pNodeA4);
  251.  
  252. Test("Test8", pNodeA1, nullptr, false);
  253.  
  254. DestroyTree(pNodeA1);
  255. }
  256.  
  257. // 树A和树B都为空
  258. void Test9()
  259. {
  260. Test("Test9", nullptr, nullptr, false);
  261. }
  262.  
  263. int main(int argc, char* argv[])
  264. {
  265. Test1();
  266. Test2();
  267. Test3();
  268. Test4();
  269. Test5();
  270. Test6();
  271. Test7();
  272. Test8();
  273. Test9();
  274.  
  275. return ;
  276. }

测试代码

剑指offer——面试题26:判断二叉树B是否为二叉树A的子结构的更多相关文章

  1. 【剑指Offer面试题】 九度OJ1385:重建二叉树

    题目链接地址: pid=1385">http://ac.jobdu.com/problem.php?pid=1385 题目1385:重建二叉树 时间限制:1 秒内存限制:32 兆特殊判 ...

  2. 剑指Offer面试题39(Java版):二叉树的深度

    题目:输入一棵二叉树的根节点,求该数的深度. 从根节点到叶结点依次进过的结点(含根,叶结点)形成树的一条路径,最长路径的长度为树的深度. 比如.例如以下图的二叉树的深度为4.由于它从根节点到叶结点的最 ...

  3. 剑指Offer:面试题23——从上往下打印二叉树(java实现)

    问题描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 按照层次遍历的方法,使用队列辅助. 1.将根结点加入队列. 2.循环出队,打印当前元素,若该结点有左子树,则将其加入队列,若 ...

  4. 剑指Offer:面试题26——复制复杂的链表(java实现)

    问题描述: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点). 思路1: 1.先复制链表节点,并用next链接起来. 2.然后对每一个结点去修改 ...

  5. 剑指offer面试题26:复杂链表的复制

    题目:请实现一个函数,复制一个复杂链表. 在复杂链表中,每个结点除了有一个next指针指向下一个结点外,还有一个sibling指针指向链表中的任意结点或者nulL 直观解法: 1.遍历链表,复制链表节 ...

  6. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  7. C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解

    剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...

  8. C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解

    剑指offer  面试题23:从上往下打印二叉树 参与人数:4853  时间限制:1秒  空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...

  9. C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解

    剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...

随机推荐

  1. http://blog.csdn.net/hongchangfirst/article/details/26004335

    悲观锁(Pessimistic Lock), 顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁.传统的 关系型数 ...

  2. .NET框架源码解读之启动CLR

    前面提到在SSCLI环境里运行.NET程序的时候,执行的命令类似java程序的执行过程,即通过clix程序解释执行.net程序.这个过程看起来跟在windows环境下执行.net程序表面上看起来不一样 ...

  3. Angular之constructor和ngOnInit差异及适用场景

    constructor会在类生成实例时调用,Angular无法控制constructor,constructor中应该只进行依赖注入而不是进行真正的业务操作 ngOnInit属于Angular生命周期 ...

  4. vs2008安装mvc3后新建项目报错 -- 类型“System.Web.Mvc.ModelClientValidationRule”同时存在

    解决方案: 找到主目录的.csproj文件,用文字编辑器打开你找到它找到 <Reference Include="System.Web.WebPages" />  &l ...

  5. 基于Quartz.net的远程任务管理系统-起绪

    Quartz.net这一个任务调度框架,相信大部分的开发者都非常的熟悉了. 往往在一个项目之中,我们会有很多的定时任务,加之多人参与编码,难免会有些难于管理等问题.为统一编写规范,以及对定时任务的管理 ...

  6. C# 4种方法计算斐波那契数列 Fibonacci

    F1: 迭代法 最慢,复杂度最高 F2: 直接法 F3: 矩阵法 参考<算法之道(The Way of Algorithm)>第38页-魔鬼序列:斐波那契序列 F4: 通项公式法 由于公式 ...

  7. 【OCP-12c】CUUG 071题库考试原题及答案解析(16)

    16.(7-5) choose the best answerThe PRODUCTS table has the following structure:Evaluate the following ...

  8. 原生JS实现JQuery的addClass和removeClass

    代码如下: document.getElementById("btn").classList.add("active"); document.getElemen ...

  9. “全栈2019”Java异常第十四章:将异常输出到文本文件中

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  10. [ActionScript 3.0] 记录几个ByteArray 十六进制 String等相互转换的方法

    /** * 通过hax数据返回ByteArray * @param hax 格式 "AA5A000100FF" */ private function getHax(hax:Str ...