```
class Solution {
public:
TreeNode* upsideDownBinaryTree(TreeNode* root) {
TreeNode *left = nullptr;
TreeNode *right = nullptr;
TreeNode *node = root;
TreeNode *head = nullptr;
while (node) {
swap(node->left, right);
swap(node->right, left);
swap(node->left, node->right);
// move to next.
head = node;
swap(node, right);
}
return head;
}
};
```