KimMinJun
Coding Note
KimMinJun
전체 방문자
오늘
어제
  • 분류 전체보기 (487)
    • 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 (432)
      • 백준 (187)
      • Programmers (105)
      • CodeUp (21)
      • STL (3)
      • 제코베 JS 100제 (50)
      • SWEA (0)
      • LeetCode (65)
    • IT (1)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Tree / 100번 / Same Tree / JS

2023. 5. 14. 19:22

< 문제 바로가기 >

 

Same Tree - LeetCode

Can you solve this real interview question? Same Tree - Given the roots of two binary trees p and q, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical, and the nodes have the

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} p
 * @param {TreeNode} q
 * @return {boolean}
 */
var isSameTree = function (p, q) {
  // p와 q 둘다 끝까지 탐색을 완료했다면,
  // 같지 않다는 조건에 걸리지 않았다는 의미이므로 true 반환
  if (!p && !q) {
    return true;
  }
  // 한쪽만 끝까지 탐색했다면 같지않다는 의미이므로 false 반환
  if (!p || !q) {
    return false;
  }
  // 둘의 node의 값이 같지않다면 false 반환
  if (p.val !== q.val) {
    return false;
  }

  // 왼쪽끼리의 node와 오른쪽끼리의 node가 모두 같다면 true, 아니라면 false
  return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};
저작자표시 (새창열림)

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

LeetCode / Tree / 199번 / Binary Tree Right Side View / JS  (0) 2023.05.14
LeetCode / Tree / 101번 / Symmetric Tree / JS  (1) 2023.05.14
LeetCode / Two Pointer / 16번 / 3Sum Closese / JS  (0) 2023.05.14
LeetCode / Dynamic Programming / 416번 / Partition Equal Subset Sum / JS  (0) 2023.05.14
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Tree / 199번 / Binary Tree Right Side View / JS
    • LeetCode / Tree / 101번 / Symmetric Tree / JS
    • LeetCode / Two Pointer / 16번 / 3Sum Closese / JS
    • LeetCode / Dynamic Programming / 416번 / Partition Equal Subset Sum / JS
    KimMinJun
    KimMinJun

    티스토리툴바