• Time:O(n)
• Space:O(h)

## C++

``````class Solution {
public:
void flatten(TreeNode* root) {
if (!root)
return;

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

TreeNode* const left = root->left;    // flattened left
TreeNode* const right = root->right;  // flattened right

root->left = nullptr;
root->right = left;

// connect the original right subtree
// to the end of new right subtree
TreeNode* rightmost = root;
while (rightmost->right)
rightmost = rightmost->right;
rightmost->right = right;
}
};
``````

## JAVA

``````class Solution {
public void flatten(TreeNode root) {
if (root == null)
return;

flatten(root.left);
flatten(root.right);

TreeNode left = root.left;   // flattened left
TreeNode right = root.right; // flattened right

root.left = null;
root.right = left;

// connect the original right subtree
// to the end of new right subtree
TreeNode rightmost = root;
while (rightmost.right != null)
rightmost = rightmost.right;
rightmost.right = right;
}
}
``````

## Python

``````class Solution:
def flatten(self, root: Optional[TreeNode]) -> None:
if not root:
return

self.flatten(root.left)
self.flatten(root.right)

left = root.left  # flattened left
right = root.right  # flattened right

root.left = None
root.right = left

# connect the original right subtree
# to the end of new right subtree
rightmost = root
while rightmost.right:
rightmost = rightmost.right
rightmost.right = right
``````

• Time:O(n)
• Space:O(h)

## C++

``````class Solution {
public:
void flatten(TreeNode* root) {
if (!root)
return;

stack<TreeNode*> stack{{root}};

while (!stack.empty()) {
root = stack.top(), stack.pop();
if (root->right)
stack.push(root->right);
if (root->left)
stack.push(root->left);
if (!stack.empty())
root->right = stack.top();
root->left = nullptr;
}
}
};
``````

## JAVA

``````class Solution {
public void flatten(TreeNode root) {
if (root == null)
return;

Deque<TreeNode> stack = new ArrayDeque<>();
stack.push(root);

while (!stack.isEmpty()) {
root = stack.pop();
if (root.right != null)
stack.push(root.right);
if (root.left != null)
stack.push(root.left);
if (!stack.isEmpty())
root.right = stack.peek();
root.left = null;
}
}
}
``````

## Python

``````class Solution:
def flatten(self, root: Optional[TreeNode]) -> None:
if not root:
return

stack = [root]

while stack:
root = stack.pop()
if root.right:
stack.append(root.right)
if root.left:
stack.append(root.left)
if stack:
root.right = stack[-1]
root.left = None
``````

• Time:O(n)
• Space:O(1)

## C++

``````class Solution {
public:
void flatten(TreeNode* root) {
if (!root)
return;

while (root) {
if (root->left) {
// find the rightmost root
TreeNode* rightmost = root->left;
while (rightmost->right)
rightmost = rightmost->right;
// rewire the connections
rightmost->right = root->right;
root->right = root->left;
root->left = nullptr;
}
// move on to the right side of the tree
root = root->right;
}
}
};
``````

## JAVA

``````class Solution {
public void flatten(TreeNode root) {
if (root == null)
return;

while (root != null) {
if (root.left != null) {
// find the rightmost root
TreeNode rightmost = root.left;
while (rightmost.right != null)
rightmost = rightmost.right;
// rewire the connections
rightmost.right = root.right;
root.right = root.left;
root.left = null;
}
// move on to the right side of the tree
root = root.right;
}
}
}
``````

## Python

``````class Solution:
def flatten(self, root: Optional[TreeNode]) -> None:
if not root:
return

while root:
if root.left:
# find the rightmost root
rightmost = root.left
while rightmost.right:
rightmost = rightmost.right
# rewire the connections
rightmost.right = root.right
root.right = root.left
root.left = None
# move on to the right side of the tree
root = root.right
``````