资讯专栏INFORMATION COLUMN

Leetcode 310. Minimum Height Trees

xuxueli / 1423人阅读

摘要:可以从头边同时进行,查看叶子节点并加入到叶子节点链表遍历一遍后,叶子节点链表为。将叶子节点保存下来。这个时候就会有第二层叶子节点那些在列表当中为的点,用同样的方法进行剥除。最后留在叶子节点里面的点即可以为根。

题目:

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.

Format The graph contains n nodes which are labeled from 0 to n - 1.
You will be given the number n and a list of undirected edges (each
edge is a pair of labels).

You can assume that no duplicate edges will appear in edges. Since all
edges are undirected, [0, 1] is the same as [1, 0] and thus will not
appear together in edges.

解法:
因为树就是两个点被一条线链接的无向图,所以先用一个list把树存成无向图的列表。可以从头边同时进行,查看叶子节点并加入到叶子节点链表(遍历一遍后,叶子节点链表size 为1)。 将叶子节点保存下来。这些叶子节点不用再查,但是剩余的中间节点,要依次查看,把跟第一层叶子节点有关的图的矩阵里对应的记录全部删除,类似于剥除最外面一圈所有的点。 这个时候就会有第二层叶子节点(那些在列表当中size为1的点),用同样的方法进行剥除。最后留在叶子节点list里面的点即可以为根。

代码:

public List findMinHeightTrees(int n, int[][] edges) {
        List leaves = new ArrayList<>();
        if (n == 1) {
            leaves.add(0);
            return leaves;
        }
        
        List> adj = new ArrayList<>();
        for(int i = 0; i < n; i++) adj.add(new HashSet<>());
        for(int[] edge : edges){
            adj.get(edge[0]).add(edge[1]);
            adj.get(edge[1]).add(edge[0]);
        }
        for(int i = 0; i < n; i++) 
         if(adj.get(i).size()==1) leaves.add(i);
        while(n > 2){
            n-=leaves.size();
            List newLeaves = new ArrayList<>();
            for(int i : leaves){
                for(int j : adj.get(i)){
                    adj.get(j).remove(i);
                    if(adj.get(j).size() ==1) newLeaves.add(j);
                }
            }
            leaves = newLeaves;
        }
        return leaves;
    }

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66472.html

相关文章

  • leetcode310. Minimum Height Trees

    摘要:现在要求在这样一棵生成树中,找到生成树的高度最低的所有根节点。然后利用邻接表的相关属性来判断当前节点是否是叶节点。度数为一的顶点就是叶节点。这里使用异或的原因是对同一个值进行两次异或即可以回到最初值。 题目 For an undirected graph with tree characteristics, we can choose any node as the root. The...

    xiaoxiaozi 评论0 收藏0
  • Minimum Height Trees

    摘要:题目链接图的题,和差不多。来解,每次放入只有一个的现在的。然后直到只剩最上面一层。注意考虑单独的和别的不相连。比如这种情况,就有三个点都不和别的相连。还有的时候就要返回。 Minimum Height Trees 题目链接:https://leetcode.com/problems... 图的题,和course schedule差不多。bfs来解,每次放入只有一个edge的node(现...

    joyqi 评论0 收藏0
  • [LeetCode] 675. Cut Off Trees for Golf Event

    Problem You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-negative 2D map, in this map: 0 represents the obstacle cant be reached.1 represents the ground ...

    MudOnTire 评论0 收藏0
  • [LeetCode] 253. Meeting Rooms II

    Problem Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required. Example 1: Input: [[0, 30],[5,...

    mengera88 评论0 收藏0
  • [LeetCode] 96. Unique Binary Search Trees I &

    Unique Binary Search Trees Problem Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Given n = 3, there are a total of 5 unique BSTs. 1 3 3...

    nidaye 评论0 收藏0

发表评论

0条评论

xuxueli

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<