面试题
拷贝二叉树
前序遍历非递归
二叉树的镜像递归
二叉树的镜像非递归
求二叉树中结点的个数
获取二叉树中叶子结点的个数
求二叉树中K层结点的个数
求二叉树的高度
其中用到的队列
栈的基本操作
二叉树的基本操作
Queue.h
#pragma once
typedef char BTDataType;
typedef struct BinTreeNode
{
struct BinTreeNode* pLeft;
struct BinTreeNode* pRight;
BTDataType _data;
}BTNode, *PBTNode;
// 构建二叉树的结点
PBTNode BuyBinTreeNode(BTDataType data);
// 创建二叉树
void _CreateBinTree(PBTNode* pRoot, const BTDataType* array, int size, int* index, BTDataType invalid);
void CreateBinTree(PBTNode* pRoot, const BTDataType* array, int size, BTDataType invalid);
// 拷贝二叉树
PBTNode CopyBinTree(PBTNode pRoot);
// 销毁二叉树
void DestroyBinTree(PBTNode *pRoot);
// 前序遍历递归
void PreOrder(PBTNode pRoot);
// 前序遍历非递归
void PreOrderNor(PBTNode pRoot);
// 中序遍历递归
void InOrder(PBTNode pRoot);
// 后续遍历递归
void PostOrder(PBTNode pRoot);
// 层序遍历
void LevelOrder(PBTNode pRoot);
// 二叉树的镜像递归
void MirrorBinTree(PBTNode pRoot);
// 二叉树的镜像非递归
void MirrorBinTreeNor(PBTNode pRoot);
// 求二叉树中结点的个数
int BinTreeSize(PBTNode pRoot);
// 获取二叉树中叶子结点的个数
int GetLeafCount(PBTNode pRoot);
// 求二叉树中K层结点的个数
int GetKLevelNode(PBTNode pRoot, int K);
// 求二叉树的高度
int Height(PBTNode pRoot);
Queue.c
#pragma once
#include"BinTree.h"
#include"Queue.h"
#include"Stack.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
PBTNode BuyBinTreeNode(BTDataType data)
{
PBTNode pnew = NULL;
pnew = (PBTNode)malloc(sizeof(BTNode));
if (NULL == pnew)
return NULL;
pnew->_data = data;
pnew->pLeft = NULL;
pnew->pRight = NULL;
return pnew;
}
void CreateBinTree(PBTNode* pRoot, const BTDataType* array, int size, BTDataType invalid)
{
int x = 0;
_CreateBinTree(pRoot, array, size, &x, invalid);
}
void _CreateBinTree(PBTNode* pRoot, const BTDataType* array, int size, int* index, BTDataType invalid)
{
int *x = 0;
assert(pRoot);
assert(array);
if ((*index < size)&&array[*index]!=invalid)
{
(*pRoot) = BuyBinTreeNode(array[*index]);
++(*index);
_CreateBinTree(&(*pRoot)->pLeft, array, size, index, invalid);
++(*index);
_CreateBinTree(&(*pRoot)->pRight, array, size, index, invalid);
}
}
void PreOrder(PBTNode pRoot)
{
if (pRoot == NULL)
return;
else
{
printf("%c ", pRoot->_data);
PreOrder(pRoot->pLeft);
PreOrder(pRoot->pRight);
}
}
面试题
void PreOrderNor(PBTNode pRoot)
{
PBTNode pCur = NULL;
Stack s;
Init(&s);
if (pRoot == NULL)
return;
else
{
pushBack(&s, pRoot);
while (!isEmpty(&s))
{
pCur = top(&s);
pop(&s);
printf("%c ", pCur->_data);
if (pCur->pRight)
pushBack(&s,pCur->pRight);
if (pCur->pLeft)
pushBack(&s, pCur->pLeft);
}
}
}
void InOrder(PBTNode pRoot)
{
if (pRoot == NULL)
return;
else
{
InOrder(pRoot->pLeft);
printf("%c ", pRoot->_data);
InOrder(pRoot->pRight);
}
}
void PostOrder(PBTNode pRoot)
{
if (pRoot == NULL)
return;
else
{
PostOrder(pRoot->pLeft);
PostOrder(pRoot->pRight);
printf("%c ", pRoot->_data);
}
}
void LevelOrder(PBTNode pRoot)
{
Queue q;
PBTNode pCur = NULL;
if (NULL == pCur)
return;
QueueInit(&q);
QueuePush(&q, pRoot);
while(!QueueEmpty(&q))
{
pCur = QueueFrontData(&q);
QueuePop(&q);
printf("%c ", pCur->_data);
if (pCur->pLeft)
QueuePush(&q, pCur->pLeft);
if (pCur->pRight)
QueuePush(&q, pCur->pRight);
}
QueueDestory(&q);
}
void DestroyBinTree(PBTNode *pRoot)
{
assert(pRoot);
if (*pRoot == NULL)
return;
if (&(*pRoot)->pLeft)
DestroyBinTree(&(*pRoot)->pLeft);
if (&(*pRoot)->pRight)
DestroyBinTree(&(*pRoot)->pRight);
free(*pRoot);
*pRoot = NULL;
}
void MirrorBinTree(PBTNode pRoot)
{
if (NULL == pRoot)
return;
else
{
PBTNode t = pRoot->pLeft;
pRoot->pLeft = pRoot->pRight;
pRoot->pRight = t;
MirrorBinTree(pRoot->pLeft);
MirrorBinTree(pRoot->pRight);
}
}
void MirrorBinTreeNor(PBTNode pRoot)
{
Queue q;
if (NULL == pRoot)
return;
QueueInit(&q);
QueuePush(&q, pRoot);
while (!QueueEmpty(&q))
{
PBTNode pCur = QueueFrontData(&q);
PBTNode pTemp = pCur->pLeft;
pCur->pLeft = pCur->pRight;
pCur->pRight = pTemp;
if (pRoot->pLeft)
QueuePush(&q, pRoot->pLeft);
if (pCur->pRight)
QueuePush(&q, pCur->pRight);
QueuePop(&q);
}
QueueDestory(&q);
}
int BinTreeSize(PBTNode pRoot)
{
if (pRoot == NULL)
return 0;
return BinTreeSize(pRoot->pLeft) + BinTreeSize(pRoot->pRight) + 1;
}
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);
}
int GetKLevelNode(PBTNode pRoot, int K)
{
if (K < 1 || NULL == pRoot)
return 0;
if (1 == K)
return 1;
return GetKLevelNode(pRoot->pLeft, K - 1) +
GetKLevelNode(pRoot->pRight, K - 1);
}
int Height(PBTNode pRoot)
{
if (NULL == pRoot)
return 0;
int HightL = Height(pRoot->pLeft);
int HightR = Height(pRoot->pRight);
return HightL > HightR ? HightL + 1 : HightR + 1;
}
PBTNode CopyBinTree(PBTNode pRoot)
{
PBTNode pnew = NULL;
if (pRoot == NULL)
return NULL;
else
{
pnew = BuyBinTreeNode(pRoot->_data);
pnew = BuyBinTreeNode(pRoot->pLeft->_data);
pnew = BuyBinTreeNode(pRoot->pRight->_data);
}
return pnew;
}