AcWing 49. 二叉搜索树与双向链表
原题链接
中等
作者:
一凡
,
2019-04-13 10:59:35
,
所有人可见
,
阅读 1035
//参考 https://cuijiahua.com/blog/2017/12/basis_26.html
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* convert(TreeNode* root) {
TreeNode* pLast=NULL;
ConvertNode(root,&pLast);
TreeNode* pHead = pLast;
while(pHead!=NULL && pHead->left!=NULL){
pHead = pHead->left;
}
return pHead;
}
void ConvertNode(TreeNode* pNode,TreeNode** pLast){
if(pNode==NULL) return;
if(pNode->left!=NULL){
ConvertNode(pNode->left,pLast);
}
//连接左右指针
pNode->left = (*pLast);
if((*pLast)!=NULL){
(*pLast)->right = pNode;
}
//更新尾节点
*pLast = pNode;
if(pNode->right!=NULL){
ConvertNode(pNode->right,pLast);
}
}
};