Create A2265.java
This commit is contained in:
1 parent
09312816eb
commit
32e6c500e5
1 file changed
+25
@@ -0,0 +1,25 @@
|
||||
package com.hong.www;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class A2265 {
|
||||
public int[] bfs(TreeNode root,ArrayList<Integer> temp){
|
||||
int[] a=new int[]{0,0};
|
||||
int[] b=new int[]{0,0};
|
||||
if(root.left!=null){
|
||||
a=bfs(root.left,temp);
|
||||
}
|
||||
if(root.right!=null){
|
||||
b=bfs(root.right,temp);
|
||||
}
|
||||
if(((a[0]+b[0]+root.val)/(a[1]+b[1]+1))==root.val){
|
||||
temp.add(1);
|
||||
}
|
||||
return new int[]{a[0]+b[0]+root.val,a[1]+b[1]+1};
|
||||
}
|
||||
public int averageOfSubtree(TreeNode root) {
|
||||
ArrayList<Integer> temp =new ArrayList<>();
|
||||
bfs(root, temp);
|
||||
return temp.size();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user