剑指offer——面试题26:判断二叉树B是否为二叉树A的子结构
- #include"iostream"
- #include"stdio.h"
- #include"math.h"
- using namespace std;
- struct BinaryTreeNode
- {
- double m_Value;
- BinaryTreeNode* m_pLeft;
- BinaryTreeNode* m_pRight;
- };
- BinaryTreeNode* CreateBinaryTreeNode(double value)
- {
- BinaryTreeNode* pNode=new BinaryTreeNode();
- pNode->m_Value=value;
- pNode->m_pLeft=nullptr;
- pNode->m_pRight=nullptr;
- return pNode;
- }
- void ConnectTreeNodes(BinaryTreeNode* pParent,BinaryTreeNode* pLeft,BinaryTreeNode* pRight)
- {
- if(pParent!=nullptr)
- {
- pParent->m_pLeft=pLeft;
- pParent->m_pRight=pRight;
- }
- }
- void PrintTreeNode(const BinaryTreeNode* pNode)
- {
- if(pNode!=nullptr)
- {
- cout<<"value of this node is:"<<pNode->m_Value<<endl;
- if(pNode->m_pLeft!=nullptr)
- cout<<"value of its left child is:"<<pNode->m_pLeft->m_Value<<endl;
- else
- cout<<"left child is nullptr."<<endl;
- if(pNode->m_pRight!=nullptr)
- cout<<"value of its right child is:"<<pNode->m_pRight->m_Value<<endl;
- else
- cout<<"right child is nullptr."<<endl;
- }
- else
- cout<<"this node is nullptr."<<endl;
- cout<<endl;
- }
- void PrintTree(const BinaryTreeNode* pRoot)
- {
- PrintTreeNode(pRoot);
- if(pRoot!=nullptr)
- {
- if(pRoot->m_pLeft!=nullptr)
- PrintTreeNode(pRoot->m_pLeft);
- if(pRoot->m_pRight!=nullptr)
- PrintTreeNode(pRoot->m_pRight);
- }
- }
- void DestroyTree(BinaryTreeNode* pRoot)
- {
- if(pRoot!=nullptr)
- {
- BinaryTreeNode* pLeft=pRoot->m_pLeft;
- BinaryTreeNode* pRight=pRoot->m_pRight;
- delete pRoot;
- pRoot=nullptr;
- DestroyTree(pLeft);
- DestroyTree(pRight);
- }
- }
- bool Equal(const double &a,const double &b)
- {
- if(fabs(a-b)<0.0000001)
- return true;
- return false;
- }
- bool DoesTreeAHaveTreeB(BinaryTreeNode* pRootA,BinaryTreeNode* pRootB)
- {
- if(pRootB==nullptr)
- return true;
- if(pRootA==nullptr)
- return false;
- if(Equal(pRootA->m_Value,pRootB->m_Value))
- {
- return DoesTreeAHaveTreeB(pRootA->m_pLeft,pRootB->m_pLeft)&&DoesTreeAHaveTreeB(pRootA->m_pRight,pRootB->m_pRight);
- }
- else
- {
- return false;
- }
- }
- bool HasSubTree(BinaryTreeNode* pRootA,BinaryTreeNode* pRootB)
- {
- if(pRootB==nullptr)
- return false;
- if(pRootA==nullptr)
- return false;
- bool result=false;
- if(Equal(pRootA->m_Value,pRootB->m_Value))
- {
- result=DoesTreeAHaveTreeB(pRootA,pRootB);
- }
- if(!result)
- {
- result=HasSubTree(pRootA->m_pLeft,pRootB);
- }
- if(!result)
- {
- result=HasSubTree(pRootA->m_pRight,pRootB);
- }
- return result;
- }
函数
- #include"BinaryTree.h"
- // ====================测试代码====================
- void Test(char* testName, BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2, bool expected)
- {
- if(HasSubTree(pRoot1, pRoot2) == expected)
- printf("%s passed.\n", testName);
- else
- printf("%s failed.\n", testName);
- }
- // 树中结点含有分叉,树B是树A的子结构
- // 8 8
- // / \ / \
- // 8 7 9 2
- // / \
- // 9 2
- // / \
- // 4 7
- void Test1()
- {
- BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA6 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA7 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
- ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
- ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);
- BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3);
- Test("Test1", pNodeA1, pNodeB1, true);
- DestroyTree(pNodeA1);
- DestroyTree(pNodeB1);
- }
- // 树中结点含有分叉,树B不是树A的子结构
- // 8 8
- // / \ / \
- // 8 7 9 2
- // / \
- // 9 3
- // / \
- // 4 7
- void Test2()
- {
- BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA6 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA7 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3);
- ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5);
- ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7);
- BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3);
- Test("Test2", pNodeA1, pNodeB1, false);
- DestroyTree(pNodeA1);
- DestroyTree(pNodeB1);
- }
- // 树中结点只有左子结点,树B是树A的子结构
- // 8 8
- // / /
- // 8 9
- // / /
- // 9 2
- // /
- // 2
- // /
- //
- void Test3()
- {
- BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeA1, pNodeA2, nullptr);
- ConnectTreeNodes(pNodeA2, pNodeA3, nullptr);
- ConnectTreeNodes(pNodeA3, pNodeA4, nullptr);
- ConnectTreeNodes(pNodeA4, pNodeA5, nullptr);
- BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeB1, pNodeB2, nullptr);
- ConnectTreeNodes(pNodeB2, pNodeB3, nullptr);
- Test("Test3", pNodeA1, pNodeB1, true);
- DestroyTree(pNodeA1);
- DestroyTree(pNodeB1);
- }
- // 树中结点只有左子结点,树B不是树A的子结构
- // 8 8
- // / /
- // 8 9
- // / /
- // 9 3
- // /
- // 2
- // /
- //
- void Test4()
- {
- BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeA1, pNodeA2, nullptr);
- ConnectTreeNodes(pNodeA2, pNodeA3, nullptr);
- ConnectTreeNodes(pNodeA3, pNodeA4, nullptr);
- ConnectTreeNodes(pNodeA4, pNodeA5, nullptr);
- BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeB1, pNodeB2, nullptr);
- ConnectTreeNodes(pNodeB2, pNodeB3, nullptr);
- Test("Test4", pNodeA1, pNodeB1, false);
- DestroyTree(pNodeA1);
- DestroyTree(pNodeB1);
- }
- // 树中结点只有右子结点,树B是树A的子结构
- // 8 8
- // \ \
- // 8 9
- // \ \
- // 9 2
- // \
- // 2
- // \
- // 5
- void Test5()
- {
- BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeA1, nullptr, pNodeA2);
- ConnectTreeNodes(pNodeA2, nullptr, pNodeA3);
- ConnectTreeNodes(pNodeA3, nullptr, pNodeA4);
- ConnectTreeNodes(pNodeA4, nullptr, pNodeA5);
- BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeB1, nullptr, pNodeB2);
- ConnectTreeNodes(pNodeB2, nullptr, pNodeB3);
- Test("Test5", pNodeA1, pNodeB1, true);
- DestroyTree(pNodeA1);
- DestroyTree(pNodeB1);
- }
- // 树A中结点只有右子结点,树B不是树A的子结构
- // 8 8
- // \ \
- // 8 9
- // \ / \
- // 9 3 2
- // \
- // 2
- // \
- // 5
- void Test6()
- {
- BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA5 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeA1, nullptr, pNodeA2);
- ConnectTreeNodes(pNodeA2, nullptr, pNodeA3);
- ConnectTreeNodes(pNodeA3, nullptr, pNodeA4);
- ConnectTreeNodes(pNodeA4, nullptr, pNodeA5);
- BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB4 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeB1, nullptr, pNodeB2);
- ConnectTreeNodes(pNodeB2, pNodeB3, pNodeB4);
- Test("Test6", pNodeA1, pNodeB1, false);
- DestroyTree(pNodeA1);
- DestroyTree(pNodeB1);
- }
- // 树A为空树
- void Test7()
- {
- BinaryTreeNode* pNodeB1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB3 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeB4 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeB1, nullptr, pNodeB2);
- ConnectTreeNodes(pNodeB2, pNodeB3, pNodeB4);
- Test("Test7", nullptr, pNodeB1, false);
- DestroyTree(pNodeB1);
- }
- // 树B为空树
- void Test8()
- {
- BinaryTreeNode* pNodeA1 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA2 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA3 = CreateBinaryTreeNode();
- BinaryTreeNode* pNodeA4 = CreateBinaryTreeNode();
- ConnectTreeNodes(pNodeA1, nullptr, pNodeA2);
- ConnectTreeNodes(pNodeA2, pNodeA3, pNodeA4);
- Test("Test8", pNodeA1, nullptr, false);
- DestroyTree(pNodeA1);
- }
- // 树A和树B都为空
- void Test9()
- {
- Test("Test9", nullptr, nullptr, false);
- }
- int main(int argc, char* argv[])
- {
- Test1();
- Test2();
- Test3();
- Test4();
- Test5();
- Test6();
- Test7();
- Test8();
- Test9();
- return ;
- }
测试代码
剑指offer——面试题26:判断二叉树B是否为二叉树A的子结构的更多相关文章
- 【剑指Offer面试题】 九度OJ1385:重建二叉树
题目链接地址: pid=1385">http://ac.jobdu.com/problem.php?pid=1385 题目1385:重建二叉树 时间限制:1 秒内存限制:32 兆特殊判 ...
- 剑指Offer面试题39(Java版):二叉树的深度
题目:输入一棵二叉树的根节点,求该数的深度. 从根节点到叶结点依次进过的结点(含根,叶结点)形成树的一条路径,最长路径的长度为树的深度. 比如.例如以下图的二叉树的深度为4.由于它从根节点到叶结点的最 ...
- 剑指Offer:面试题23——从上往下打印二叉树(java实现)
问题描述: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. 思路: 按照层次遍历的方法,使用队列辅助. 1.将根结点加入队列. 2.循环出队,打印当前元素,若该结点有左子树,则将其加入队列,若 ...
- 剑指Offer:面试题26——复制复杂的链表(java实现)
问题描述: 输入一个复杂链表(每个节点中有节点值,以及两个指针,一个指向下一个节点,另一个特殊指针指向任意一个节点). 思路1: 1.先复制链表节点,并用next链接起来. 2.然后对每一个结点去修改 ...
- 剑指offer面试题26:复杂链表的复制
题目:请实现一个函数,复制一个复杂链表. 在复杂链表中,每个结点除了有一个next指针指向下一个结点外,还有一个sibling指针指向链表中的任意结点或者nulL 直观解法: 1.遍历链表,复制链表节 ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- C++版 - 剑指offer 面试题24:二叉搜索树BST的后序遍历序列(的判断) 题解
剑指offer 面试题24:二叉搜索树的后序遍历序列(的判断) 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则返回true.否则返回false.假设输入的数组的任意两个 ...
- C++版 - 剑指offer 面试题23:从上往下打印二叉树(二叉树的层次遍历BFS) 题解
剑指offer 面试题23:从上往下打印二叉树 参与人数:4853 时间限制:1秒 空间限制:32768K 提交网址: http://www.nowcoder.com/practice/7fe2 ...
- C++版 - 剑指Offer 面试题39:二叉树的深度(高度)(二叉树深度优先遍历dfs的应用) 题解
剑指Offer 面试题39:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
随机推荐
- http://blog.csdn.net/hongchangfirst/article/details/26004335
悲观锁(Pessimistic Lock), 顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁.传统的 关系型数 ...
- .NET框架源码解读之启动CLR
前面提到在SSCLI环境里运行.NET程序的时候,执行的命令类似java程序的执行过程,即通过clix程序解释执行.net程序.这个过程看起来跟在windows环境下执行.net程序表面上看起来不一样 ...
- Angular之constructor和ngOnInit差异及适用场景
constructor会在类生成实例时调用,Angular无法控制constructor,constructor中应该只进行依赖注入而不是进行真正的业务操作 ngOnInit属于Angular生命周期 ...
- vs2008安装mvc3后新建项目报错 -- 类型“System.Web.Mvc.ModelClientValidationRule”同时存在
解决方案: 找到主目录的.csproj文件,用文字编辑器打开你找到它找到 <Reference Include="System.Web.WebPages" /> &l ...
- 基于Quartz.net的远程任务管理系统-起绪
Quartz.net这一个任务调度框架,相信大部分的开发者都非常的熟悉了. 往往在一个项目之中,我们会有很多的定时任务,加之多人参与编码,难免会有些难于管理等问题.为统一编写规范,以及对定时任务的管理 ...
- C# 4种方法计算斐波那契数列 Fibonacci
F1: 迭代法 最慢,复杂度最高 F2: 直接法 F3: 矩阵法 参考<算法之道(The Way of Algorithm)>第38页-魔鬼序列:斐波那契序列 F4: 通项公式法 由于公式 ...
- 【OCP-12c】CUUG 071题库考试原题及答案解析(16)
16.(7-5) choose the best answerThe PRODUCTS table has the following structure:Evaluate the following ...
- 原生JS实现JQuery的addClass和removeClass
代码如下: document.getElementById("btn").classList.add("active"); document.getElemen ...
- “全栈2019”Java异常第十四章:将异常输出到文本文件中
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...
- [ActionScript 3.0] 记录几个ByteArray 十六进制 String等相互转换的方法
/** * 通过hax数据返回ByteArray * @param hax 格式 "AA5A000100FF" */ private function getHax(hax:Str ...