A pythonic BFS solution

  • 0
    class Solution(object):
        def averageOfLevels(self, root):
            ans = []
            lvl = [root]
            while lvl:
                ans.append(sum(n.val for n in lvl) / float(len(lvl)))
                lvl = [c for n in lvl for c in [n.left, n.right] if c]
            return ans

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.