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)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

LeetCode / Tree / 543번 / Diameter of Binary Tree / JS
PS/LeetCode

LeetCode / Tree / 543번 / Diameter of Binary Tree / JS

2023. 4. 28. 17:36

< 문제 바로가기 >

 

Diameter of Binary Tree - LeetCode

Can you solve this real interview question? Diameter of Binary Tree - Given the root of a binary tree, return the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path

leetcode.com

 

< 문제 간단설명 >

diameter를 직역하면 지름이라는 뜻인데... 이 문제에서 지름을 반환하면 된다(?).

문제에서 나타내는 diameter란 트리내에서 임의로 어떤 노드 두개를 선택했을 때의 그 사이가 가장 긴 거리를 나타낸다.

위와 같은 트리가 주어졌을 때, 여기서의 diameter는 4(or 5) -> 2 -> 1 -> 3을 나타낸다. 혹은 거꾸로도 가능하다.

 

/**
 * 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 {number}
 */
var diameterOfBinaryTree = function(root) {
    let max = 0;

    const DFS = (node) => {
        if(node === null) {
            return 0;
        }

        let left = DFS(node.left);
        let right = DFS(node.right);

        // 왼쪽 subtree와 오른쪽 subtree중 더 depth가 큰 것
        max = Math.max(max, left + right);
        return Math.max(left, right) + 1;
    }

    DFS(root);

    return max;
};
저작자표시 (새창열림)

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

LeetCode / Binary Search / 74번 / Search a 2D Matrix / JS  (0) 2023.04.28
LeetCode / Tree / 437번 / Path Sum III / JS  (0) 2023.04.28
LeetCode / Tree / 110번 / Balanced Binary Tree / JS  (0) 2023.04.28
LeetCode / Tree / 226번 / Invert Binary Tree / JS  (0) 2023.04.28
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Binary Search / 74번 / Search a 2D Matrix / JS
    • LeetCode / Tree / 437번 / Path Sum III / JS
    • LeetCode / Tree / 110번 / Balanced Binary Tree / JS
    • LeetCode / Tree / 226번 / Invert Binary Tree / JS
    KimMinJun
    KimMinJun

    티스토리툴바