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)

블로그 메뉴

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

공지사항

인기 글

태그

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

최근 댓글

최근 글

hELLO · Designed By 정상우.
KimMinJun

Coding Note

PS/LeetCode

LeetCode / Tree / 1161번 / Maximum Level Sum of a Binary Tree / JS

2023. 6. 17. 13:57

< 문제 바로가기 >

 

Maximum Level Sum of a Binary Tree - LeetCode

Can you solve this real interview question? Maximum Level Sum of a Binary Tree - Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on. Return the smallest level x such that the sum of all the values of node

leetcode.com

 

< 문제 간단설명 >

트리에서 각 층을 이루는 노드들의 합을 구했을 때, 그 합이 가장 큰 층이 몇 층인지 반환하는 문제이다.

 

/**
 * 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 maxLevelSum = function (root) {
  let maxSum = -Infinity; // 최대 합
  let maxSumLevel = 1; // 최대 합을 이루는 레벨
  let level = 1; // 현재 레벨
  let queue = [root];

  // BFS
  while (queue.length) {
    let queueLength = queue.length;

    // 현재 큐에 담겨 있는, 한 레벨에 있는 노드들의 합
    let sum = queue.reduce((acc, cur) => acc + cur.val, 0);
    // 최대 합을 이룬다면,
    if (sum > maxSum) {
      // 최대합 갱신
      maxSum = sum;
      // 최대합 레벨 갱신
      maxSumLevel = level;
    }

    // 한 층에 있는 모든 노드 순회
    for (let i = 0; i < queueLength; i += 1) {
      let node = queue.shift();

      // 노드의 left가 있다면 queue에 삽입
      if (node.left) {
        queue.push(node.left);
      }
      // 노드의 right가 있다면 queue에 삽입
      if (node.right) {
        queue.push(node.right);
      }
    }

    level += 1;
  }

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

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

LeetCode / Array / 2090번 / K Radius Subarray Averages / JS  (0) 2023.06.21
LeetCode / Array / 1732번 / Find the Highest Altitude / JS  (0) 2023.06.21
LeetCode / Tree / 530번 / Minimum Absolute Difference in BST / JS  (0) 2023.06.17
LeetCode / Matrix / 2352번 / Equal Row and Column Pairs / JS  (0) 2023.06.17
    'PS/LeetCode' 카테고리의 다른 글
    • LeetCode / Array / 2090번 / K Radius Subarray Averages / JS
    • LeetCode / Array / 1732번 / Find the Highest Altitude / JS
    • LeetCode / Tree / 530번 / Minimum Absolute Difference in BST / JS
    • LeetCode / Matrix / 2352번 / Equal Row and Column Pairs / JS
    KimMinJun
    KimMinJun

    티스토리툴바