#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;
}