import java.util.*; //这次差点儿忘了这个
class Node { //这个是题目给好的
    int val;
    ArrayList<Node> children;//subtre就是名字变了
    public Node(int val){
        this.val = val;
        children = new ArrayList<Node>();
    }
}
//这个类是自己写的,要不不好找,两个成员变量分别是当前的总和和人数
class resultType{
    int sum;
    int size;
    public resultType(int sum, int size){
        this.sum = sum;
        this.size = size;
    }
}
public class Solution {
    //两个全局变量用来找最小的平均值,和对应的节点
    private static double resAve = Double.MIN_VALUE;
    private static Node result;
    public static Node getHighAve(Node root){
        if (root == null) return null;
        dfs(root);
        return result;
    }
    //后序遍历递归。//root 叫present
    private static resultType dfs(Node root){
        // 这里必须先把叶子节点刨掉,注意看我的手法,其实没什么。
        if (root.children == null || root.children.size() == 0){
            return new resultType(root.val, 1);
        }
        //把当前root的材料都准备好
        int curSum = root.val;
        int curSize = 1;
        //注意了这里开始遍历小朋友了
        for (Node child : root.children) {
            resultType tmp = dfs(child);
            //每次遍历一个都把sum,count都加上,更新
            curSum += tmp.sum;
            curSize += tmp.size;
        }
        double curAve = (double) curSum/curSize;
        //这里看一下最大值要不要更新
        if (resAve < curAve){
            resAve = curAve;
            result = root;
        }
        return new resultType(curSum,curSize);
    }
    //这回测试的code行数有点儿多。
    public static void main(String[] args) {
        Node root = new Node(1);
        Node l21 = new Node(2);
        Node l22 = new Node(3);
        Node l23 = new Node(4);
        Node l31 = new Node(5);
        Node l32 = new Node(5);
        Node l33 = new Node(5);
        Node l34 = new Node(5);
        Node l35 = new Node(5);
        Node l36 = new Node(5);

        l21.children.add(l31);
        l21.children.add(l32);

        l22.children.add(l33);
        l22.children.add(l34);

        l23.children.add(l35);
        l23.children.add(l36);

        root.children.add(l21);
        root.children.add(l22);
        root.children.add(l23);

        Node res = getHighAve(root);
        System.out.println(res.val + " " + resAve);
    }
}

results matching ""

    No results matching ""