637. Average of Levels in Binary Tree
s1. Depth first traverse, record every node’s (value, level)
s2. Width first traverse, keep two queues as parent level and kid level.
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.
Example 1:
1 | Input: |
Note:
- The range of node’s value is in the range of 32-bit signed integer.
Soulution:
- DFS
1 | class Solution: |
- WFS
1 | class Solution: |