// return total number of nodes in the BST
int makeVine(TreeNode *root, int cnt = 0) {
TreeNode* n = root->right;
while (n != nullptr) {
if (n->left != nullptr) { // rotate all n's left child to its parent (parent->right = n)
TreeNode* tmp = n;
n = n->left;
tmp->left = n->right;
n->right = tmp;
root->right = n;
} // every time only rotate one node to its parent
else {
++cnt;
root = n;
n = n->right;
}
}
return cnt;
}