把已排序的双向链表转变成平衡二叉树

#include "stdafx.h"
#include "vector"
#include "list"

typedef struct node
{
    int val;
    struct node *pre;
    struct node *rear;
}NODE;



NODE *getMiddleNode(NODE *pStart, NODE *pEnd)
{
    NODE *pFast;
    NODE *pSlow;

    if (!pStart)
        return NULL;

    if (pStart->pre == pEnd)
        return NULL;

    if (pStart == pEnd)
    {
        pStart->pre  = NULL;
        pStart->rear = NULL;
        return pStart;
    }

    pFast = pStart;
    pSlow = pStart;

    while ((pFast != pEnd) && (pFast ->rear != pEnd))
    {
        pFast = pFast->rear->rear;
        pSlow = pSlow->rear;
    }

    return pSlow;
}
NODE* convertLinkToTree(NODE *pHead, NODE *pRear)
{
    NODE *pMiddle = getMiddleNode(pHead, pRear);

    if (pMiddle)
    {
        pMiddle->pre  = convertLinkToTree(pHead, pMiddle->pre);
        pMiddle->rear = convertLinkToTree(pMiddle->rear, pRear);
    }

    return pMiddle;
}
    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/martin_liang/article/details/8019066
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞