剑指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:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
随机推荐
- .net core利用MySqlBulkLoader大数据批量导入MySQL
最近用core写了一个数据迁移小工具,从SQLServer读取数据,加工后导入MySQL,由于数据量太过庞大,数据表都过百万,常用的dapper已经无法满足.三大数据库都有自己的大数据批量导入数据的方 ...
- 关于InvokeMethod Activity的异步调用
讨论地址:http://www.cnblogs.com/foundation/archive/2009/12/17/1626617.html 结论是IsCompleted的设置被忽略,看代码里注释 u ...
- Qracle 11g 插图安装
1.下载两个包 然后把包二的内容放入包一里 2.管理员身份运行setup 3.选择去掉复选框 4.下一步,然后选择是 5.选择第一项,然后选择下一步 6.个人使用选桌面类 7.不要放在C盘中 8.设置 ...
- Linux 基础教程 38-文件下载
什么是wget wget用原始帮助里面的英文来讲就是:The non-interactive network downloader,非交互式网络下载器.它支持HTTP.HTTPS.FTP等协议 ...
- 两段 PHP 代码比较优劣
// 代码一 public function getPCA($level = false) { $results = array(); $where = $level ? " where f ...
- ASP.NET Core2集成Office Online Server(OWAS)实现办公文档的在线预览与编辑(支持word\excel\ppt\pdf等格式)
Office Online Server是微软开发的一套基于Office实现在线文档预览编辑的技术框架(支持当前主流的浏览器,且浏览器上无需安装任何插件,支持word.excel.ppt.pdf等文档 ...
- spring 3.0版本以上jar包使用以及依赖关系
本文转载自:http://blog.csdn.net/huiwenjie168/article/details/8477837 spring.jar是包含有完整发布的单个jar包,spring.jar ...
- ASP.NET2.0 Newtonsoft.Json 操作类分享
JSON 是现在比较流行的数据交互格式,NET3.0+有自带类处理JSON,2.0的话需要借助Newtonsoft.Json来完成,不然自己写的话,很麻烦. 网上搜索下载 Newtonsoft.Jso ...
- C#基础笔记(第十九天)
1.CSS 层叠样式表 对HTML的补充实现网页内容和页面效果的彻底分离1.内联样式表(在标签内设置元素的样式)<p style="background:red; font-size: ...
- how to remote debug in vs 2013
first download the debugger tools "rtools_setup_x64" start C:\Program Files\Microsoft Visu ...