😎풀이

  1. root 존재 여부 확인
  2. root.leftroot.right 교환
  3. 재귀적으로 root.leftroot.right의 자식 요소 탐색
  4. root 노드 반환
function invertTree(root: TreeNode | null): TreeNode | null {
    if(!root) return null;
    [root.left, root.right] = [root.right, root.left];
    invertTree(root.left);
    invertTree(root.right);
    return root;
};
profile
내 지식을 공유할 수 있는 대담함

0개의 댓글