1161. 最大层内元素和
给你一个二叉树的根节点 root。设根节点位于二叉树的第 1 层,而根节点的子节点位于第 2 层,依此类推。
请你找出层内元素之和 最大 的那几层(可能只有一层)的层号,并返回其中 最小 的那个。
链接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
   | 
 
 
 
 
 
 
 
 
 
 
 
 
  function maxLevelSum(root: TreeNode | null): number {   const nodeQue: TreeNode[] = [root]   let max = Number.NEGATIVE_INFINITY   let depth = 0   let res = 0   while(nodeQue.length) {     const nodes = nodeQue.splice(0)     const len = nodes.length     let floorTotal = 0     depth++     for(let i = 0; i < len; i++) {       const node = nodes[i]       floorTotal += node.val       node.left && nodeQue.push(node.left)       node.right && nodeQue.push(node.right)     }     if (max < floorTotal) {       max = floorTotal       res = depth     }   }   return res };
 
  |