题解 | #二叉搜索树与双向链表#
二叉搜索树与双向链表
https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5
忘记前驱和后继的含义了。。。后面查了才发现自己排反了。
class Solution {
public:
TreeNode* Convert(TreeNode* pRootOfTree) {
if (!pRootOfTree) return pRootOfTree;
if (!pRootOfTree->left && !pRootOfTree->right) return pRootOfTree;
TreeNode* head = pRootOfTree;
TreeNode* lhead = pRootOfTree->left, * rhead = pRootOfTree->right;
TreeNode* tail;
if (lhead) {
head = Convert(lhead);
for (tail = head; tail->right; tail = tail->right);
tail->right = pRootOfTree;
pRootOfTree->left = tail;
}
if (rhead) {
rhead = Convert(rhead);
rhead->left = pRootOfTree;
pRootOfTree->right = rhead;
}
return head;
}
};
