[snap]company tree

2017-01-19  本文已影响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]);
    }
    
}
上一篇 下一篇

猜你喜欢

热点阅读