在leetcode 如下题目中使用队列
637. 二叉树的层平均值:
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def averageOfLevels(self, root: Optional[TreeNode]) -> List[float]: ans = [] queue = deque([root]) while queue: sum = 0 cnt = len(queue) for i in range(cnt): node = queue.popleft() sum += node.val if node.left: queue.append(node.left) if node.right: queue.append(node.right) ans.append(sum/cnt) return ans
标签:node,right,val,队列,self,queue,使用,python3,left From: https://www.cnblogs.com/jyfyonghu/p/18445574