[Leetcode] Binary tree-- 637. Average of Levels in Binary Tree
来源:互联网 发布:mini metro mac下载 编辑:程序博客网 时间:2024/05/16 09:28
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.
Example 1:
Input: 3 / \ 9 20 / \ 15 7Output: [3, 14.5, 11]Explanation:The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 1
Solution:
# it actually examines the knowledge of level order
#refer to the similar problem : 102. Binary Tree Level Order Traversal
1 if not root: 2 return [] 3 d = collections.deque() 4 d.append(root) 5 resLst = [] 6 while(len(d)): 7 n = len(d) 8 lst = [] 9 tmpSum = 0.010 count = n11 while(n > 0):12 node = d.popleft()13 tmpSum += node.val14 if node.left:15 d.append(node.left)16 if node.right:17 d.append(node.right)18 n -= 119 resLst.append(tmpSum/count)20 21 return resLst
阅读全文
0 0
- [Leetcode] Binary tree-- 637. Average of Levels in Binary Tree
- LeetCode average Of Levels in Binary Tree
- Average of Levels in Binary Tree(leetcode)
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- 637. Average of Levels in Binary Tree
- PyCharm快捷键
- 决策树ID3
- Session ID
- C/C++面试经典
- 用dijkstra获得全部最短路径
- [Leetcode] Binary tree-- 637. Average of Levels in Binary Tree
- vSphere,ESXi,vCenter之间的关系
- socket C/C++编程(2)Samba工具——将Windows本地文件 Automatically 同步到Linux上(反之亦成立)
- org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)新的异常解决方案
- 二叉树已知前序和后序求中序的相关补充
- Intent的多种作用(传activity&组件调用)
- Android MediaRecorder自定义分辨率
- ORACLE ORA-00600: 内部错误代码, 参数: [kokbcvb1]与merge into
- HDU 6103 Kirinriki(尺取法)