剑指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:二叉树的深度(高度) 题目:输入一棵二叉树的根结点,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度.例如:输入二叉树 ...
随机推荐
- UVA 11865 Stream My Contest (二分+最小树形图)
题意:给定一个网络,一个服务器,其他的是客户机,有 m 条连线,每条有一个带宽和花费(单向边),让你用不超过 c 的花费,使得 0 到 所有的机器都能到达,并且使得最小带宽最大. 析:很明显是二分题, ...
- NAOQI API之学习笔记
https://www.jianshu.com/p/e84f38e45bf5 NAOQI OS是软银pepper和nao机器人的核心操作系统,NAOQI API提供了访问机器人的各种传感器设备接口以及 ...
- Array对象的创建及其操作方法
一.创建数组,即实例化数组对象 有三种方式:1. new Array(); 2.new Array(size); ...
- Ubuntu命令基础
Ubuntu命令基础 1.打开终端窗口快捷键. Ctrl+alt+t 2.更新设置root密码. $sudo passwd root 3.切换到root用户用su,前提是自己设置了root密码(看 ...
- Spring学习(六)——集成memcached客户端
memcached是高性能的分布式内存缓存服务器.许多Web应用都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示. 但随着数据量的增大.访问的集中,就会出现RDBMS的负担加重.数据 ...
- Caliburn.Micro 资源随时添加
Caliburn.Micro – Hello World http://buksbaum.us/2010/08/01/caliburn-micro-hello-world/ http://blog.c ...
- django drf Token验证
https://www.django-rest-framework.org/api-guide/authentication/#basicauthentication 1.INSTALLED_APPS ...
- LightOJ 1197 LightOJ 1197(大区间素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1197 题目大意: 就是给你一个区间[a,b]让你求这个区间素数的个数 但a.b的值太大没法直接进 ...
- 2-初步了解C#-类与对象
本篇博客对应视频讲解 回顾 我们在第一篇文章中讲了编程中最基本的内容,如输入输出.字符串处理.数字类型计算.分支及循环结构等.无论学习什么语言,这些内容都是相通的. 本篇博客主要演示列表(List)的 ...
- FastDFS 安装与使用
FastDFS 安装与使用 1. 什么是 FastDFS FastDFS是一个开源的高性能分布式文件系统(DFS). 它的主要功能包括:文件存储,文件同步和文件访问,以及高容量和负载平衡的设计. Fa ...