#include <iostream>
using namespace std;
struct BinaryTreeNode {
int m_nValue;
struct BinaryTreeNode* m_pLeft;
struct BinaryTreeNode* m_pRight;
};
BinaryTreeNode* convert(BinaryTreeNode *pRoot) {
if(pRoot == NULL)
return NULL;
BinaryTreeNode *pLastNodeInList = NULL;
convert(pRoot,&pLastNodeInList);
// pLastNodeInList指向双向链表的尾结点,
// 我们需要返回头结点
BinaryTreeNode *pHeadOfList = pLastNodeInList;
while(pHeadOfList != NULL && pHeadOfList->m_pLeft != NULL)
pHeadOfList = pHeadOfList->m_pLeft;
return pHeadOfList;
}
void convert(BinaryTreeNode *p,BinaryTreeNode** pLast) {
if(p != NULL) {
if(p->m_pLeft != NULL) {
convert(p->m_pLeft,pLast);
}
p->m_pLeft = *pLast;
if(*pLast != NULL) {
(*pLast)->m_pRight = p;
}
*pLast = p;
if(p->m_pRight != NULL) {
convert(p->m_pRight,pLast);
}
}
}
剑指Offer-27-二叉排序树转双向链表
最新推荐文章于 2021-08-24 16:12:23 发布