二叉树的面试题

1.创建二叉树

先创建根节点,后创建左子树,再最后是右子树

//构建二叉树的结点
PBTNode BuyBinTreeNode(BTDataType data)
{
	PBTNode pNewNode = NULL;
	pNewNode = (PBTNode)malloc(sizeof(BTNode));
	if(NULL == pNewNode)
	{
		printf("申请失败!!!\n");
		return NULL;
	}
	pNewNode->_data = data;
	pNewNode->_pLeft = NULL;
	pNewNode->_pRight = NULL;

	return pNewNode;
}

//创建二叉树数据节点(按前序遍历的方式创建)
void _CreateBinTree(PBTNode* pRoot,const BTDataType* array,
					int size,int* index,BTDataType invalid)
					//(*index)是访问数组array的下标
{
	assert(index);
	assert(pRoot);
	if(NULL == pRoot || array[*index] == invalid)
		return;
	else if((*index) < size && invalid != array[*index])
	{
		//创建根节点
		*pRoot = BuyBinTreeNode(array[*index]);

		//创建根节点的左子树
		++(*index);
		_CreateBinTree(&(*pRoot)->_pLeft,array,size,index,invalid);
		
		//创建根节点的右子树
		++(*index);
		_CreateBinTree(&(*pRoot)->_pRight,array,size,index,invalid);

	}
}

//创建二叉树结构
void CreateBinTree(PBTNode* pRoot,const BTDataType* array,
				   int size,BTDataType invalid)
{
	int index = 0;
	_CreateBinTree(pRoot,array,size,&index,invalid);
}

2.求二叉树的高度

先递归的分别求出左子树和右子树的高度,然后两个比较哪个高,就返回哪个,最后记得+1,因为这里规定根节点高度为1

// 求二叉树的高度 
int Height(PBTNode pRoot)
{
	if(NULL == pRoot)
		return 0;
	return Height(pRoot->_pLeft)>Height(pRoot->_pRight) ? (1+Height(pRoot->_pLeft)):(1+Height(pRoot->_pRight));
}

3.求二叉树中节点的个数

// 求二叉树中结点的个数 
int BinTreeSize(PBTNode pRoot)
{
	if(pRoot == NULL)
		return 0;
	return BinTreeSize(pRoot->_pLeft) + BinTreeSize(pRoot->_pRight) + 1;
}

4.求二叉树叶子结点的个数

有三种情况:            1)空树,没有节点

                              2)只有根节点,它自己是一个叶子结点

                             3)不是空树也不是只有一个根节点,那么递归求解

                                

// 获取二叉树中叶子结点的个数 
int GetLeafCount(PBTNode pRoot)
{
	if(NULL == pRoot)
		return 0;
	if( (NULL == (pRoot->_pLeft)) && (NULL == (pRoot->_pRight)) )
		return 1;
	return GetLeafCount(pRoot->_pLeft) + GetLeafCount(pRoot->_pRight);
}

5.求二叉树中第K层节点的个数

// 求二叉树中K层结点的个数 
int GetKLevelNode(PBTNode pRoot, int K)
{
	if(NULL == pRoot)
		return 0;
	if(1 == K)
		return 1;
	return GetKLevelNode(pRoot->_pLeft,K-1) + GetKLevelNode(pRoot->_pRight,K-1);
}

6.判断一个节点是否在一棵二叉树中

//判断一个结点是否在一颗二叉树中
int IsNodeInBinTree(PBTNode pRoot,PBTNode pNode)
{
	int ret = 0;
	if(NULL == pRoot || NULL == pNode)
		return 0;
	if(pRoot == pNode)
		return 1;
	if(ret = IsNodeInBinTree(pRoot->_pLeft,pNode))
		return ret;
	return IsNodeInBinTree(pRoot->_pRight,pNode);
}

7.判断一棵二叉树是否是完全二叉树

//判断一棵二叉树是否是完全二叉树(层序遍历)
int IsCompeleteBinTree(PBTNode pRoot)
{
	SQueue q;
	int flag = 0;//标记仅有左孩子的结点
	if(NULL == pRoot)
		return 1;//空树也是一颗完全二叉树

	SQueueInit(&q);
	SQueuePush(&q,pRoot);

	while(!SQueueEmpty(&q))
	{
		PBTNode pCur = SQueueFrontData(&q);
	
		
		if(flag)//找到了左孩子的结点
		{
			if(pCur->_pLeft || pCur->_pRight)
				return 0;//返回假	
		}
		else
		{
			if(NULL == pCur->_pLeft && NULL != pCur->_pRight)//左孩子没有而右孩子存在,一定不为完全二叉树
				return 0;//返回假
			else if(NULL != pCur->_pLeft && NULL == pCur->_pRight)//只有左孩子
			{
				SQueuePush(&q,pCur->_pLeft);
				flag = 1;//标记仅有左孩子的结点
			}
			else if(pCur->_pLeft && pCur->_pRight)
			{
				SQueuePush(&q,pCur->_pLeft);
				SQueuePush(&q,pCur->_pRight);
			}
			else
				flag = 1;
		}
			SQueuePop(&q);
	}
	return 1;
}

8.二叉树的镜像(递归&&非递归)

// 二叉树的镜像递归 
void MirrorBinTree(PBTNode pRoot)
{
	if(NULL == pRoot)
		return;
	else
	{
		PBTNode tmp = pRoot->_pLeft;
		pRoot->_pLeft = pRoot->_pRight;
		pRoot->_pRight = tmp;

		MirrorBinTree(pRoot->_pLeft);
		MirrorBinTree(pRoot->_pRight);
	}
}

void Swap(PBTNode *Left,PBTNode *Right)
{
	PBTNode tmp = NULL;
	assert(Left);
	assert(Right);

	tmp = *Left;
	*Left = *Right;
	*Right = tmp;

}

// 二叉树的镜像非递归 
void MirrorBinTreeNor(PBTNode pRoot)
{
	SQueue q;
	if(NULL == pRoot)
		return;

	SQueueInit (&q);
	SQueuePush(&q,pRoot);
	while(!SQueueEmpty(&q))
	{
		PBTNode pCur = SQueueFrontData(&q);

		Swap(&pRoot->_pLeft,&pRoot->_pRight);
		
		if(pCur->_pLeft)
			SQueuePush(&q,pCur->_pLeft);
		if(pCur->_pRight)
			SQueuePush(&q,pCur->_pRight);
		SQueuePop(&q);
	}
	SQueueDestory(&q);
}

猜你喜欢

转载自blog.csdn.net/ijn842/article/details/80353803