Skip to content

Commit 1408c43

Browse files
committed
solve problem Average Of Levels In Binary Tree
1 parent f5c0b87 commit 1408c43

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ All solutions will be accepted!
5555
|83|[Remove Duplicates From Sorted List](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/)|[java/py/js](./algorithms/RemoveDuplicatesFromSortedList)|Easy|
5656
|82|[Remove Duplicates From Sorted List II](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/description/)|[java/py/js](./algorithms/RemoveDuplicatesFromSortedListII)|Medium|
5757
|811|[Subdomain Visit Count](https://leetcode-cn.com/problems/subdomain-visit-count/description/)|[java/py/js](./algorithms/SubdomainVisitCount)|Easy|
58+
|637|[Average Of Levels In Binary Tree](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/description/)|[java/py/js](./algorithms/AverageOfLevelsInBinaryTree)|Easy|
5859

5960
# Database
6061
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Average Of Levels In Binary Tree
2+
This problem is easy to solve
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public List<Double> averageOfLevels(TreeNode root) {
12+
List<Double> res = new ArrayList<Double>();
13+
List<TreeNode> level = new ArrayList<TreeNode>();
14+
if (root != null) level.add(root);
15+
16+
while (level.size() > 0) {
17+
List<TreeNode> tempLevel = new ArrayList<TreeNode>();
18+
Double sum = 0D;
19+
for (TreeNode node : level) {
20+
sum += node.val;
21+
if (node.left != null) tempLevel.add(node.left);
22+
if (node.right != null) tempLevel.add(node.right);
23+
}
24+
25+
res.add(sum / level.size());
26+
level = tempLevel;
27+
}
28+
return res;
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number[]}
11+
*/
12+
var averageOfLevels = function(root) {
13+
14+
let res = [],
15+
level = root === null ? [] : [root]
16+
17+
while (level.length > 0) {
18+
res.push(level.reduce((x, y) => x + y.val, 0) / level.length)
19+
temp = []
20+
level.forEach(node => {
21+
if (node.left) temp.push(node.left)
22+
if (node.right) temp.push(node.right)
23+
})
24+
level = temp
25+
}
26+
27+
return res
28+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def averageOfLevels(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: List[float]
13+
"""
14+
if not root:
15+
return []
16+
res = []
17+
level = [root]
18+
while len(level) > 0:
19+
res.append(sum([x.val for x in level]) / (len(level) * 1.0))
20+
temp = []
21+
for node in level:
22+
if node.left:
23+
temp.append(node.left)
24+
if node.right:
25+
temp.append(node.right)
26+
level = temp
27+
return res

0 commit comments

Comments
 (0)