一样的剑指 Offer 36. 二叉搜索树与双向链表_zj-CSDN博客,中序遍历一边遍历一边调整指针
class Solution {
public:
//left, right对应前驱、后继
Node *head = NULL, *cur = NULL;//当前节点,上一个节点
void inorder(Node* root){//中序遍历
if(root->left) inorder(root->left);
if(!head){
head = root;
cur = root;
}else{
cur->right = root;
root->left = cur;
cur = root;
}
if(root->right) inorder(root->right);
}
Node* treeToDoublyList(Node* root) {
if(!root) return NULL;
inorder(root);
head->left = cur;//首尾相连
cur->right = head;
return head;
}
};