PS/LeetCode

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

KimMinJun 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;
};