Old/LeetCode
LeetCode - 226. Invert Binary Tree
mang_dev
2020. 11. 12. 00:58
문제
풀이
해당 노드의 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;
}
}