美文网首页
[snap]company tree

[snap]company tree

作者: 秋_轩 | 来源:发表于2017-01-19 12:08 被阅读0次
import java.util.List;
import java.util.Stack;

public class Solution {
    class PersonNode{
        int val;
        List<PersonNode> people;
        
        PersonNode(int val){
            this.val = val;
        }
    }
    
    // 0 for choose, 1 for not
    public int[] helper(PersonNode root){
        int[] res = new int[2];
        if(root.people.size() == 0){
            res[0] = root.val;
            return res;
        }
        
        res[0] = root.val;
        for(PersonNode ps : root.people){
            int[] psRes = helper(ps);
            res[0] += psRes[1];
            res[1] += Math.max(psRes[0],psRes[1]);
        }
        
        return res;
    }
    
    
    public int companyTree(PersonNode root){
        int[] res = helper(root);
        return Math.max(res[0],res[1]);
    }
    
}

相关文章

网友评论

      本文标题:[snap]company tree

      本文链接:https://www.haomeiwen.com/subject/aplibttx.html