Skip to content

Commit 63c064e

Browse files
committed
Tree -public List<Double> averageOfLevels(TreeNode root) {
1 parent aec31a7 commit 63c064e

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/leetcode2018/Trees.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import java.util.List;
66
import java.util.Queue;
77

8-
import amazon.TreeNode;
9-
108

119
public class Trees {
1210

@@ -146,6 +144,26 @@ public boolean hasPathSum(TreeNode root, int sum) {
146144
return hasPathSum(root.left, sum-root.val) || hasPathSum(root.right,sum-root.val);
147145
}
148146

147+
public List<Double> averageOfLevels(TreeNode root) {
148+
List<Double> result = new ArrayList<Double>();
149+
if(root==null) return result;
150+
Queue<TreeNode> q = new LinkedList<TreeNode>();
151+
q.offer(root);
152+
while(!q.isEmpty()){
153+
double sum = 0;
154+
int size= q.size();
155+
for(int i =0; i< size; i++){
156+
TreeNode currentNode = q.poll();
157+
sum= sum+ currentNode.val;
158+
if(currentNode.left!=null) q.offer(currentNode.left);
159+
if(currentNode.right!=null) q.offer(currentNode.right);
160+
}
161+
double overage= (double)sum/size;
162+
result.add(overage);
163+
}
164+
return result;
165+
}
166+
149167
}
150168

151169
class TreeNode{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy