树的子结构

这篇博客探讨了树结构中指针可能为NULL的情况及其处理方式,结合《剑指Offer》中的相关问题进行深入解析。
摘要由CSDN通过智能技术生成
#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值