``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void flatten(TreeNode* root) {
if(!root) return;

flatten(root->left);
flatten(root->right);

if(root->left != nullptr)
{
//save the origin root->right
TreeNode* right = root->right;

//change left to right
root->right = root->left;
root->left = nullptr;

//find the rightest
while(root->right)
root = root->right;

root->right = right;
}
}
};``````

``````/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode() : val(0), left(nullptr), right(nullptr) {}
*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void flatten(TreeNode* root) {
TreeNode* now = root;
while(now){
if(now->left)
{
TreeNode* pre = now->left;
while(pre->right)
pre = pre->right;

pre->right = now->right;
now->right = now->left;
now->left = nullptr;
}
now = now->right;
}
}
};``````