杭电1054-后续遍历解决


import java.util.*;

/**
 * Created by handsome.guy on 2014/12/6.
 */
public class Main {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        scanner.useDelimiter("(\\s+)|(:\\()|(\\)\\s+)");
        while(scanner.hasNext()){
            int nodeNum=scanner.nextInt();
            Map<Integer,List<Integer>> adjMap=new HashMap<Integer, List<Integer>>();
            Integer rootName=null;
            for(int i=0;i<nodeNum;++i){
                int node=scanner.nextInt();
                if(rootName==null){
                    rootName=node;
                }
                int adjNum=scanner.nextInt();
                List<Integer> nodes=new ArrayList<Integer>(adjNum);
                for(int j=0;j<adjNum;++j){
                    nodes.add(scanner.nextInt());
                }
                adjMap.put(node,nodes);
            }
            Node root=TreeFactory.parseTree(adjMap,rootName);
            System.out.println(Work.calculate(root));
        }
    }
    private static class Work{
        public static int calculate(Node root){
            postTraversal(root);
            return root.getMinSentinel();
        }
        public static void postTraversal(Node root){
            List<Node> children=root.getChildren();
            if(children==null || children.isEmpty()){
                return;
            }
            for(Node node:children){
                postTraversal(node);
            }
            int rootFilledResult=1;
            for(Node node:children){
                rootFilledResult+=node.getMinSentinel();
            }
            int rootEmptyResult=children.size();
            for(Node node:children){
                List<Node> subChildren=node.getChildren();
                for(Node n:subChildren){
                    rootEmptyResult+=n.getMinSentinel();
                }
            }
            int min=rootFilledResult<rootEmptyResult?
                    rootFilledResult:rootEmptyResult;
            root.setMinSentinel(min);
            return;
        }
    }
    private static class TreeFactory{
        public static Node parseTree(Map<Integer,List<Integer>>adjMap,Integer rootName){
            Map<Integer,Node> allNode=new HashMap<Integer, Node>();
            for(Integer key:adjMap.keySet()){
                Node t=new Node();
                t.setName(key);
                t.setChildren(new ArrayList<Node>());
                t.setMinSentinel(0);
                allNode.put(key,t);
            }
            for(Integer key:adjMap.keySet()){
                List<Integer> list=adjMap.get(key);
                for(Integer name:list){
                    allNode.get(key).getChildren().add(allNode.get(name));
                }
            }
            return allNode.get(rootName);
        }
    }
    private static class Node{
        private Integer name;
        private List<Node> children;
        private Integer minSentinel;

        public Integer getName() {
            return name;
        }

        public void setName(Integer name) {
            this.name = name;
        }

        public List<Node> getChildren() {
            return children;
        }

        public void setChildren(List<Node> children) {
            this.children = children;
        }

        public Integer getMinSentinel() {
            return minSentinel;
        }

        public void setMinSentinel(Integer minSentinel) {
            this.minSentinel = minSentinel;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值