美文网首页
图相关题目

图相关题目

作者: Phoebe_Liu | 来源:发表于2018-12-21 14:38 被阅读0次
  1. Minimum Height Trees
    For a undirected graph with tree characteristics, we can choose any node as the root. The result graph is then a rooted tree. Among all possible rooted trees, those with minimum height are called minimum height trees (MHTs). Given such a graph, write a function to find all the MHTs and return a list of their root labels.

思路:剥洋葱法。我们需要建立一个图g,是一个二维数组,其中g[i]是一个一维数组,保存了i节点可以到达的所有节点。我们开始将所有只有一个连接边的节点(叶节点)都存入到一个队列queue中,然后我们遍历每一个叶节点,通过图来找到和其相连的节点,并且在其相连节点的集合中将该叶节点删去,如果删完后此节点也也变成一个叶节点了,加入队列中,再下一轮删除。那么我们删到什么时候呢,当节点数小于等于2时候停止,此时剩下的一个或两个节点就是我们要求的最小高度树的根节点啦

class Solution {
    public List<Integer> findMinHeightTrees(int n, int[][] edges) {
        
        List<Integer> res = new ArrayList<>();
        if(n==1){
            res = new ArrayList<Integer>();
            res.add(0);
            return res;
        }
        
        // 声明+初始化
        List<List<Integer>>  graph = new ArrayList<List<Integer>>(); // 图的二位矩阵表示发 
        int[] indegree = new int[n]; // 节点的入度
        for(int i=0;i<n;i++){
            List<Integer> cur_level = new ArrayList<Integer>();
            graph.add(cur_level);
        }
        
        // 填充图数据
        Queue<Integer> queue = new LinkedList<Integer>();
        for(int[] edge: edges){
            int inNode=edge[0];
            int outNode=edge[1];
            
            // 图矩阵
            graph.get(inNode).add(outNode);
            graph.get(outNode).add(inNode);
            // 入度节点矩阵
            indegree[inNode]++;
            indegree[outNode]++;
        }
        
        // 初始化queue: 获取当前最外层的叶子节点
        for(int i=0;i<n;i++){
            if(indegree[i]==1)
                queue.offer(i);
        }
        
        while(!queue.isEmpty()){
            int size = queue.size();
            res=new ArrayList<Integer>();
            
            // 遍历当前叶子节点
            for(int i=0;i<size;i++){
                int cur_leaf = queue.poll();
                indegree[cur_leaf]--;
                res.add(cur_leaf);
                
                // 获取当前叶子节点的根节点,将根节点的入度也-1
                for(int root: graph.get(cur_leaf)){
                    indegree[root]--;
                    if(indegree[root] ==1)
                        queue.offer(root);
                }
            }
        }
        return res;
    }
}

相关文章

  • 图相关题目

    Minimum Height TreesFor a undirected graph with tree char...

  • 树和图相关的题目

    二叉树的序列化和反序列化:前根遍历,根的值在前面计算 最长回文子串:怎么判定回文串

  • 『图』找到小镇的法官997

    题目相关 原题链接:997. 找到小镇的法官 - 力扣(LeetCode) 涉及知识:图、有向图 题目难度:★ 题...

  • 『图』不邻接植花1042

    题目相关 原题链接:1042. 不邻接植花 - 力扣(LeetCode) 涉及知识:图 题目难度:★ 题目解读 由...

  • BZOJ_1001 狼抓兔子

    1.题目相关 标签:平面图最大流 题目地址:http://www.lydsy.com/JudgeOnline/pr...

  • 『图;广度优先遍历』地图分析1162

    题目相关 原题链接:1162. 地图分析 - 力扣(LeetCode) 涉及知识:图、广度优先遍历 题目难度:★★...

  • Promise相关题目

    实现一个person对象,有eat和dinner两种方法请用实例【依次类推】new Person('Tom').s...

  • web相关题目

    你觉得前端工程师的价值体现在哪? 为简化用户使用提供技术支持(交互部分) 为多个浏览器兼容性提供支持 为提高用户浏...

  • GCD相关题目

    1、以下代码结果会如何? 结果如下: 会造成死锁,主线程中【同步执行+主队列】,造成的互相等待。 2、写一个线程安...

  • LeetCode SQL 相关题目

    Delete Duplicate Emails Rising Temperature

网友评论

      本文标题:图相关题目

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