Given a binary tree, invert it. For each node:
the left child must become the right child;
the right child must become the left child.
Definition of a tree:
// Java
class TreeNode
{
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
left = NULL;
right = NULL;
}
}// C++
class TreeNode
{
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};Implement function Invert that returns the pointer to the inverted tree.
// Java
TreeNode Invert(TreeNode tree)// C++
TreeNode* Invert(TreeNode *tree)
Function Invert returns res - pointer to the inverted tree.