二叉树的基本操作与面试题

面试题
拷贝二叉树
前序遍历非递归
二叉树的镜像递归
二叉树的镜像非递归
求二叉树中结点的个数
获取二叉树中叶子结点的个数
求二叉树中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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值