【问题标题】:Find the max avg score in a N-ary Tree在 N 叉树中找到最大平均分数
【发布时间】:2016-10-29 05:55:56
【问题描述】:

我想显示 n 叉树的最大平均分数,其中平均值 = 子节点值的总和 / 子节点的数量。 我的节点是这样定义的

class NaryNode {
int value;
NaryNode parent;
List<NaryNode> children = new ArrayList<NaryNode>();

NaryNode(int x) {
    this.value = x;
}

public void addChild(NaryNode childNode) {
    childNode.parent = this;
    this.children.add(childNode);
  }

}


public class NaryTree {
public NaryNode root = new NaryNode(10);

public NaryTree() {
    root.parent = null;
}

public void traverseTree(NaryNode rootNode)// depth first
{
    System.out.println(rootNode.value);
    if (rootNode.children.size() != 0)
        for (NaryNode ch : rootNode.children)
            traverseTree(ch);
}

public static void main(String[] args) {
    NaryTree mytree = new NaryTree();

    NaryNode n2 = new NaryNode(20);
    NaryNode n3 = new NaryNode(3);
    NaryNode n4 = new NaryNode(15);

    NaryNode n5 = new NaryNode(8);
    NaryNode n6 = new NaryNode(45);
    NaryNode n7 = new NaryNode(22);

    NaryNode n8 = new NaryNode(11);
    NaryNode n9 = new NaryNode(16);
    NaryNode n10 = new NaryNode(18);

    NaryNode n11 = new NaryNode(7);

    mytree.root.addChild(n2);
    mytree.root.addChild(n3);
    mytree.root.addChild(n4);

    n2.addChild(n5);
    n2.addChild(n6);
    n2.addChild(n7);

    n3.addChild(n8);
    n3.addChild(n9);
    n3.addChild(n10);

    n4.addChild(n11);

    // mytree.traverseTree(mytree.root);
    int max = Integer.MIN_VALUE;
    int maxavg = calculateaverage(mytree.root,max);
    System.out.println(maxavg);
}

private static int calculateaverage(NaryNode root,int max) {
    int sum = 0;
    int count =0;
    if(root.children.size() == 0)
        return root.value;
    for(NaryNode cc : root.children){
        sum += calculateaverage(cc,max);
        count++;
    }
    sum = sum/count;
    if(sum>max)
        max = sum;
    return max;
}

 }

我写了下面的逻辑,但它给了我错误的答案,因为我的逻辑不正确。你能指出我哪里出错了吗?

【问题讨论】:

    标签: java algorithm data-structures tree


    【解决方案1】:

    您必须为每个有孩子的孩子更新您的 max 值。试试这个:

    private static int calculateaverage(NaryNode root,int max) {
        int sum = 0;
        int count =0;
        if(root.children.size() == 0)
            return root.value;
        for(NaryNode cc : root.children){
            if(cc.children.size() > 0){
                int tmp = calculateaverage(cc,max);
                if(tmp>max){
                    max = tmp;
                }
            }
            sum+=cc.value;
            count++;
        }
        sum = sum/count;
        if(sum>max)
            max = sum;
        return max;
    }
    

    注意:您可能希望使用doubles 作为平均值。

    【讨论】:

    • @Vinny 你为什么收回我的回答?现在是你要找的吗?如果没有,请添加更多详细信息,以便我改进答案。
    • :- 实际上,替换 NaryNode n3 = new NaryNode(3);使用 NaryNode n3 = new NaryNode(3000);并检查它。答案仍然是 25。我猜它只是检查最后一级
    猜你喜欢
    • 2011-06-03
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    相关资源
    最近更新 更多