Given the root of a binary tree, invert the tree, and return its root.
Example 1:
The number of nodes in the tree is in the range [0, 100].
-100 <= Node.val <= 100
[전략] 재귀적으로 left child 노드와 right child 노드를 치환한다.
/**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left
* = left; this.right = right; } }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode temp = root.left;
root.left = invertTree(root.right);
root.right = invertTree(temp);
return root;
}
}