KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (486)
    • ALGORITHM (11)
      • 정렬 (6)
      • 최단경로 (1)
      • 자료구조 (1)
      • 슬라이딩 윈도우 (1)
      • etc (2)
    • Git (5)
    • Web (24)
      • Vanilla JS (13)
      • TS (2)
      • React (7)
      • ETC (1)
    • React 공식문서 (번역, 공부) (11)
      • Quick Start (2)
      • Installation (0)
      • Describing the UI (9)
      • Adding Interactivity (0)
      • Managing State (0)
      • Escape Hatches (0)
    • Next.js 공식문서 (번역, 공부) (3)
      • Getting Started (2)
      • Building Your Application (1)
    • PS (431)
      • 백준 (187)
      • Programmers (104)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

  • 홈
  • 태그
  • 방명록
  • 관리

공지사항

인기 글

태그

  • 제코베 JS 100제
  • recursion
  • 수학
  • Level 2
  • Level1
  • 정렬
  • 백준
  • 그래프
  • LeetCode
  • programmers
  • js
  • Level 0
  • tree
  • 문자열
  • C
  • C++
  • string
  • 다이나믹 프로그래밍
  • codeup
  • Level 1

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Tree / 101번 / Symmetric Tree / JS

2023. 5. 14. 19:24

< 문제 바로가기 >

 

Symmetric Tree - LeetCode

Can you solve this real interview question? Symmetric Tree - Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).   Example 1: [https://assets.leetcode.com/uploads/2021/02/19/symtree1.jpg] Input: roo

leetcode.com

 

< 문제 간단설명 >

주어진 트리가 좌우대칭을 이루는지 판별하여 boolean 값을 반환하는 문제이다.

 

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function (root) {
  const isMirror = (left, right) => {
    // p와 q 둘다 끝까지 탐색을 완료했다면,
    // 같지 않다는 조건에 걸리지 않았다는 의미이므로 true 반환
    if (!left && !right) {
      return true;
    }
    // 한쪽만 끝까지 탐색했다면 같지않다는 의미이므로 false 반환
    if (!left || !right) {
      return false;
    }
    // 둘의 node의 값이 같지않다면 false 반환
    if (left.val !== right.val) {
      return false;
    }

    // 대칭 확인
    return isMirror(left.left, right.right) && isMirror(left.right, right.left);
  };

  return isMirror(root.left, root.right);
};
저작자표시

'PS > LeetCode' 카테고리의 다른 글

LeetCode / Design / 232번 / Implement Queue using Stacks / JS  (0) 2023.05.14
LeetCode / Tree / 199번 / Binary Tree Right Side View / JS  (0) 2023.05.14
LeetCode / Tree / 100번 / Same Tree / JS  (1) 2023.05.14
LeetCode / Two Pointer / 16번 / 3Sum Closese / JS  (0) 2023.05.14
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Design / 232번 / Implement Queue using Stacks / JS
    • LeetCode / Tree / 199번 / Binary Tree Right Side View / JS
    • LeetCode / Tree / 100번 / Same Tree / JS
    • LeetCode / Two Pointer / 16번 / 3Sum Closese / JS
    KimMinJun
    KimMinJun

    티스토리툴바