Really interesting BST manipulation problem!
class Solution { public: TreeNode *findConn(TreeNode *p) { TreeNode *pTmp = p; while (pTmp->right) { pTmp = pTmp->right; } return pTmp; } void flatten(TreeNode *root) { if (!root) return; TreeNode *p = root; while (p) { if (!p->left && !p->right) break; // end if (p->left && !p->right) { p->right = p->left; p->left = NULL; } if (p->left && p->right) { TreeNode *pTmpRight = p->right; TreeNode *pConn = findConn(p->left); p->right = p->left; pConn->right = pTmpRight; p->left = NULL; } p = p->right; } } };