TreeNode* convert(TreeNode* root) {
TreeNode* head = nullptr, *cur = nullptr;
function<void(TreeNode*)> _convert = [&](TreeNode* root){
if (root == nullptr) return;
if (root->left != nullptr)
_convert(root->left);
else {
if (head == nullptr) head = root;
}
if (cur != nullptr) {
cur->right = root;
root->left = cur;
}
cur = root;
_convert(root->right);
};
_convert(root);
return head;
}