Invert Binary Tree
/**
* 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 tempTree = root.left;
root.left = root.right;
root.right = tempTree;
// 왼쪽과 오른쪽 서브트리도 변경
invertTree(root.left);
invertTree(root.right);
return root;
}
}
#99클럽 #코딩테스트 준비 #개발자 취업 #항해99 #TIL