判断一棵二叉树中是否包含另一棵二叉树
2016-09-25 08:37
561 查看
以下内容经过实际上机测试,如有不对,请指正,谢谢
#include <stdio.h>
#include <stdlib.h>
#define bool int
#define true 1
#define false 0
typedef struct BinaryTreeNode{
int value;
struct BinaryTreeNode *pLeft;
struct BinaryTreeNode *pRight;
}BinaryTreeNode;
BinaryTreeNode *createTree()//先序序列建立二叉树,输入-1代表当前节点为空
{
BinaryTreeNode *treeRoot;
int num;
printf("请输入当前节点的值:\n");
scanf("%d",&num);
if(num == -1)
treeRoot = NULL;
else
{
treeRoot = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));
treeRoot->value = num;
printf("目前为左子树节点");
treeRoot->pLeft = createTree();
printf("目前为右子树节点");
treeRoot->pRight = createTree();
}
return treeRoot;
}
void xxBL(BinaryTreeNode *treeRoot) //先序遍历二叉树
{
if(treeRoot)
{
printf("%d ",treeRoot->value);
xxBL(treeRoot->pLeft);
xxBL(treeRoot->pRight);
}
}
bool hasSubTree(BinaryTreeNode *pRoot1, BinaryTreeNode *pRoot2)
{
bool result = false;
if(pRoot1 != NULL && pRoot2 != NULL)
{
if(pRoot1->value == pRoot2->value)
result = DoesTree1HaveTree2(pRoot1,pRoot2);
if(!result)
result = hasSubTree(pRoot1->pLeft,pRoot2);
if(!result)
result = hasSubTree(pRoot1->pRight,pRoot2);
}
return result;
}
bool DoesTree1HaveTree2(BinaryTreeNode *pRoot1, BinaryTreeNode *pRoot2)
{
if(pRoot2 == NULL)
return true;
if(pRoot1 == NULL)
return false;
if(pRoot1->value != pRoot2->value)
return false;
if(pRoot1->value == pRoot2->value)
return DoesTree1HaveTree2(pRoot1->pLeft,pRoot2->pLeft)
&& DoesTree1HaveTree2(pRoot1->pRight,pRoot2->pRight);
}
int main()
{
BinaryTreeNode *root1 = createTree();
/*
printf("二叉树的先序遍历序列为:\n");
xxBL(root);
*/
BinaryTreeNode *root2 = createTree();
bool bl = hasSubTree(root1,root2);
printf("%d",bl);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define bool int
#define true 1
#define false 0
typedef struct BinaryTreeNode{
int value;
struct BinaryTreeNode *pLeft;
struct BinaryTreeNode *pRight;
}BinaryTreeNode;
BinaryTreeNode *createTree()//先序序列建立二叉树,输入-1代表当前节点为空
{
BinaryTreeNode *treeRoot;
int num;
printf("请输入当前节点的值:\n");
scanf("%d",&num);
if(num == -1)
treeRoot = NULL;
else
{
treeRoot = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));
treeRoot->value = num;
printf("目前为左子树节点");
treeRoot->pLeft = createTree();
printf("目前为右子树节点");
treeRoot->pRight = createTree();
}
return treeRoot;
}
void xxBL(BinaryTreeNode *treeRoot) //先序遍历二叉树
{
if(treeRoot)
{
printf("%d ",treeRoot->value);
xxBL(treeRoot->pLeft);
xxBL(treeRoot->pRight);
}
}
bool hasSubTree(BinaryTreeNode *pRoot1, BinaryTreeNode *pRoot2)
{
bool result = false;
if(pRoot1 != NULL && pRoot2 != NULL)
{
if(pRoot1->value == pRoot2->value)
result = DoesTree1HaveTree2(pRoot1,pRoot2);
if(!result)
result = hasSubTree(pRoot1->pLeft,pRoot2);
if(!result)
result = hasSubTree(pRoot1->pRight,pRoot2);
}
return result;
}
bool DoesTree1HaveTree2(BinaryTreeNode *pRoot1, BinaryTreeNode *pRoot2)
{
if(pRoot2 == NULL)
return true;
if(pRoot1 == NULL)
return false;
if(pRoot1->value != pRoot2->value)
return false;
if(pRoot1->value == pRoot2->value)
return DoesTree1HaveTree2(pRoot1->pLeft,pRoot2->pLeft)
&& DoesTree1HaveTree2(pRoot1->pRight,pRoot2->pRight);
}
int main()
{
BinaryTreeNode *root1 = createTree();
/*
printf("二叉树的先序遍历序列为:\n");
xxBL(root);
*/
BinaryTreeNode *root2 = createTree();
bool bl = hasSubTree(root1,root2);
printf("%d",bl);
return 0;
}
相关文章推荐
- 判断二叉树是否包含另一棵二叉树或者树的子结构
- 剑指off-判断一棵二叉树是否包含另一棵
- 剑指Offer--039-平衡二叉树(判断一棵二叉树是否是平衡二叉树)[扩展附加题]
- 判断一棵二叉树是否是另一棵二叉树的子树
- 29.输入一棵二叉树,判断该二叉树是否是平衡二叉树。
- 判断一个值是否在一棵二叉树中。(注意多测几个值,看是否都能找到)
- 每日一题之判断一个节点是否在一棵二叉树中
- java 判断一棵二叉树是否为平衡二叉树
- 以二叉链表的方式创建一棵二叉树,并以非递归算法中序输出;计算二叉树的繁茂度,并判断二叉树是否为完全二叉树
- 判断一棵二叉树是否是二叉树的子树
- 输入一棵二叉树,判断该二叉树是否是平衡二叉树。
- 输入一棵二叉树,判断该二叉树是否是平衡二叉树。
- 剑指offer 输入一棵二叉树,判断该二叉树是否是平衡二叉树。
- -判断一棵二叉树是否是平衡二叉树/求一颗二叉树的镜像
- 判断一个节点是否在一棵二叉树中&判断一颗二叉树是是否是另一颗树的子树
- 判断一棵二叉树是否为平衡二叉树
- [互联网面试笔试汇总C/C++-16] 判断一棵二叉树是否是平衡二叉树
- 15-- 输入两个二叉树A和B,判断B树是否包含于A树。
- day14之判断一个节点是否在一棵二叉树中+判断一颗二叉树是是否是另一颗树的子树
- 【二叉树】判断一棵二叉树是否是平衡二叉树/求一棵二叉树的镜像/对称的二叉树