문제
풀이
해당 노드의 leftChild와 rightChild를 서로 swap하는 문제이다.
재귀함수를 이용하여 노드가 null이 아닌 경우, leaf node까지 모두 바꿔준 뒤, 해당 노드의 leftChild와 rightChild를 바꾸는 방법을 사용하였다.
코드
더보기
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null)
return null;
root.left = invertTree(root.left);
root.right = invertTree(root.right);
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
}
'Old > LeetCode' 카테고리의 다른 글
LeetCode - 746. Min Cost Climbing Stairs (0) | 2021.02.06 |
---|---|
LeetCode - 303. Range Sum Query (2) | 2021.02.05 |
LeetCode - 217. Contains Duplicate (0) | 2020.11.11 |
LeetCode - 50. Pow(x, n) // Java (0) | 2020.11.10 |
LeetCode - 19. Remove Nth Node From End of List // Java (0) | 2020.11.10 |