您的位置:首页 > 职场人生

剑指offer 面试题27 二叉搜索树转换为排序双向链表

2014-12-25 11:30 615 查看
struct BinaryTreeNode{
int data;
BinaryTreeNode *left;
BinaryTreeNode *right;
};
void Convert_core(BinaryTreeNode *pNode,BinaryTreeNode **pLastNode){
//handle left subtree
if(pNode->left)
Convert_core(pNode->left,pLastNode);
//add pNode to the end of the lsit
pNode->left=*pLastNode;
if(*pLastNode)
*pLastNode->right=pNode;
*pLastNode=pNode;
//handle right subtree
if(pNode->right)
Convert_core(pNode->right,pLastNode);
}
BinaryTreeNode* Convert(BinaryTreeNode *pRoot){
if(pRoot==NULL) return NULL;
//get last node
BinaryTreeNode *pLastNode=NULL;
Convert_core(pRoot,&pLastNode);
//get first node
BinaryTreeNode *pFirstNode=pLastNode;
while(pFirstNode->left){
pFirstNode=pFirstNode->left;
}
return pFirstNode;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: