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)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Tree / 110번 / Balanced Binary Tree / JS

2023. 4. 28. 17:31

< 문제 바로가기 >

 

Balanced Binary Tree - LeetCode

Can you solve this real interview question? Balanced Binary Tree - Given a binary tree, determine if it is height-balanced.   Example 1: [https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg] Input: root = [3,9,20,null,null,15,7] Output: true Exam

leetcode.com

 

< 문제 간단설명 >

균형잡힌 이진트리인지 boolean 값으로 반환하는 문제이다. 여기서 균형잡힌 이진트리란, 왼쪽과 오른쪽의 subtree의 depth가 1보다 크게, 즉 2이상 차이나지 않는 트리이다.

 

/**
 * 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 isBalanced = function(root) {
    let result = true;

    const DFS = (node, height) => {
        // 빈 subtree라면 height 0 반환
        if(node === null) {
            return 0;
        }

        let leftHeight = DFS(node.left, height + 1);
        let rightHeight = DFS(node.right, height + 1);

        // height 차이가 1보다 크다면 false
        if(Math.abs(leftHeight - rightHeight) > 1) {
            result = false;
        }

        // 왼쪽과 오른쪽중 큰 height에 + 1을 해서 반환
        // backtracking 을 하면서 높이를 구해줌
        return Math.max(leftHeight, rightHeight) + 1;
    }

    DFS(root, 0);

    return result;
};

 

저작자표시 (새창열림)

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

LeetCode / Tree / 437번 / Path Sum III / JS  (0) 2023.04.28
LeetCode / Tree / 543번 / Diameter of Binary Tree / JS  (0) 2023.04.28
LeetCode / Tree / 226번 / Invert Binary Tree / JS  (0) 2023.04.28
LeetCode / Greedy / 621번 / Task Scheduler / JS  (0) 2023.04.28
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Tree / 437번 / Path Sum III / JS
    • LeetCode / Tree / 543번 / Diameter of Binary Tree / JS
    • LeetCode / Tree / 226번 / Invert Binary Tree / JS
    • LeetCode / Greedy / 621번 / Task Scheduler / JS
    KimMinJun
    KimMinJun

    티스토리툴바