#include<stdio.h>
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
BinaryTreeNode* createBinaryTreeNode(int value)
{
BinaryTreeNode* pNewNode = new BinaryTreeNode();
pNewNode->m_nValue = value;
pNewNode->m_pLeft = NULL;
pNewNode->m_pRight = NULL;
return pNewNode;
}
void connectBinaryTreeNode(BinaryTreeNode* pParent,BinaryTreeNode* pLeftChild,
BinaryTreeNode* pRightChild)
{
if(!pParent)
return;
pParent->m_pLeft = pLeftChild;
pParent->m_pRight = pRightChild;
}
/*
bool hasTheSubTeeWithRoot(BinaryTreeNode* pBigTree, BinaryTreeNode* pSmallTree);
//判断pSmallTree是不是pBigTree的子树
bool isSubtreeInBinaryTree(BinaryTreeNode* pBigTree, BinaryTreeNode* pSmallTree)
{
if(pSmallTree == NULL || pBigTree == NULL)
return false;
bool result = false;
bool result1 = false;
bool result2 = false;
result = hasTheSubTeeWithRoot(pBigTree,pSmallTree);
if(pBigTree->m_pLeft)
result1 = isSubtreeInBinaryTree(pBigTree->m_pLeft,pSmallTree);
if(pBigTree->m_pRight)
result2 = isSubtreeInBinaryTree(pBigTree->m_pRight,pSmallTree);
return (result || result1 || result2);
}
//两棵树已包含根节点为条件,判断是否为子树
bool hasTheSubTeeWithRoot(BinaryTreeNode* pBigTree, BinaryTreeNode* pSmallTree)
{
if(pSmallTree == NULL)
return true;
if
树的子结构
最新推荐文章于 2022-04-20 20:23:54 发布
这篇博客探讨了树结构中指针可能为NULL的情况及其处理方式,结合《剑指Offer》中的相关问题进行深入解析。
摘要由CSDN通过智能技术生成