Leetcode 1161 - Maximum Level Sum of a Binary Tree

題目

Problem#

給你一個 Binary Tree 問你能不能回傳一個最小的 depth $x$ 其中 depth $x$ 的所有 node 總和 $y$ 最大

測資限制#

  • $1 \le n \le 10^4$
  • $-10^5 \le val \le 10^5$

想法#

BFS 遍歷,要記得每個 node 的深度,當往下走訪(left, right)時,depth 要加一
建個 array sum[i] 其中 i 是圖的 depth

  • 時間複雜度: $\mathcal{O}(n)$
  • 空間複雜度: $\mathcal{O}(n)$

AC Code#