EasyRating 1186
637. Average of Levels in Binary Tree
treedepth-first-searchbreadth-first-searchbinary-tree
解題說明
C++ 解法
複雜度分析
虛擬碼
1. Initialize result as empty array
2. If root is null: return result
3. Initialize queue q and push root
4. While q is not empty:
a. sz = q.size() (number of nodes at current level)
b. sum = 0.0 (use double)
c. For i from 0 to sz-1:
i. node = q.front(); q.pop()
ii. sum += node.val
iii. If node.left exists: push node.left
iv. If node.right exists: push node.right
d. Append (sum / sz) to result
5. Return result