Skip to content

Commit 867286a

Browse files
solves average of levels in binary tree
1 parent f1e25ec commit 867286a

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

Diff for: README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,18 @@
165165
| 624 | 🔒 [Maximum Distance in Arrays](https://leetcode.com/problems/maximum-distance-in-arrays) | |
166166
| 628 | [Maximum Product of Three Numbers](https://leetcode.com/problems/maximum-product-of-three-numbers) | [![Java](assets/java.png)](src/MaximumProductOfThreeNumbers.java) [![Python](assets/python.png)](python/maximum_product_of_three_numbers.py) |
167167
| 633 | [Sum Square Numbers](https://leetcode.com/problems/sum-of-square-numbers) | [![Java](assets/java.png)](src/SumOfSquareNumbers.java) [![Python](assets/python.png)](python/sum_of_squares_numbers.py) |
168-
| 637 | [Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree) | |
168+
| 637 | [Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree) | [![Java](assets/java.png)](src/AverageLevelsOfBinaryTree.java) [![Python](assets/python.png)](python/)
169169
| 643 | [Maximum Average SubArray I](https://leetcode.com/problems/maximum-average-subarray-i) | [![Java](assets/java.png)](src/MaximumAverageSubArrayI.java) [![Python](assets/python.png)](python/maximum_average_subarray_I.py) |
170170
| 645 | [Set Mismatch](https://leetcode.com/problems/set-mismatch) | [![Java](assets/java.png)](src/SetMismatch.java) [![Python](assets/python.png)](python/set_mismatch.py) |
171171
| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst) | [![Java](assets/java.png)](src/TwoSumIVInputIsABST.java) [![Python](assets/python.png)](python/two_sum_iv.py) |
172172
| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin) | [![Java](assets/java.png)](src/RobotReturnToOrigin.java) [![Python](assets/python.png)](python/robot_return_to_origin.py) |
173173
| 661 | [Image Smoother](https://leetcode.com/problems/image-smoother) | [![Java](assets/java.png)](src/ImageSmoother.java) [![Python](assets/python.png)](python/image_smoother.py) |
174+
| 637 | [Average of Levels in Binary Tree](https://leetcode.com/problems/average-of-levels-in-binary-tree) | [![Java](assets/java.png)](src/AverageLevelsOfBinaryTree.java) [![Python](assets/python.png)](python/) |
175+
| 643 | [Maximum Average SubArray I](https://leetcode.com/problems/maximum-average-subarray-i) | |
176+
| 645 | [Set Mismatch](https://leetcode.com/problems/set-mismatch) | |
177+
| 653 | [Two Sum IV - Input is a BST](https://leetcode.com/problems/two-sum-iv-input-is-a-bst) | |
178+
| 657 | [Robot Return to Origin](https://leetcode.com/problems/robot-return-to-origin) | |
179+
| 661 | [Image Smoother](https://leetcode.com/problems/image-smoother) | |
174180
| 665 | [Non Deceasing Array](https://leetcode.com/problems/non-decreasing-array) | |
175181
| 669 | [Trim a Binary Search Tree](https://leetcode.com/problems/trim-a-binary-search-tree) | |
176182
| 671 | [Second Minimum Node in Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree) | |

Diff for: src/AverageLevelsOfBinaryTree.java

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.ArrayList;
2+
import java.util.LinkedList;
3+
import java.util.List;
4+
import java.util.Queue;
5+
6+
public class AverageLevelsOfBinaryTree {
7+
public List<Double> averageOfLevels(TreeNode root) {
8+
Queue<TreeNode> queue = new LinkedList<>();
9+
queue.add(root);
10+
queue.add(null);
11+
int count = 0;
12+
List<Double> result = new ArrayList<>();
13+
result.add(0.0);
14+
while (!queue.isEmpty()) {
15+
TreeNode current = queue.poll();
16+
if (current == null && queue.isEmpty()) break;
17+
if (current == null) {
18+
queue.add(null);
19+
result.set(result.size() - 1, result.get(result.size() - 1) / count);
20+
result.add(0.0);
21+
count = 0;
22+
continue;
23+
}
24+
count++;
25+
if (current.left != null) queue.add(current.left);
26+
if (current.right != null) queue.add(current.right);
27+
result.set(result.size() - 1, result.get(result.size() - 1) + current.val);
28+
}
29+
result.set(result.size() - 1, result.get(result.size() - 1) / count);
30+
return result;
31+
}
32+
}

0 commit comments

Comments
 (0)