国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

leetcode310. Minimum Height Trees

xiaoxiaozi / 592人閱讀

摘要:現(xiàn)在要求在這樣一棵生成樹(shù)中,找到生成樹(shù)的高度最低的所有根節(jié)點(diǎn)。然后利用鄰接表的相關(guān)屬性來(lái)判斷當(dāng)前節(jié)點(diǎn)是否是葉節(jié)點(diǎn)。度數(shù)為一的頂點(diǎn)就是葉節(jié)點(diǎn)。這里使用異或的原因是對(duì)同一個(gè)值進(jìn)行兩次異或即可以回到最初值。

題目
For an 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.

Example 1 :

Input: n = 4, edges = [[1, 0], [1, 2], [1, 3]]

        0
        |
        1
       / 
      2   3 

Output: [1]
Example 2 :

Input: n = 6, edges = [[0, 3], [1, 3], [2, 3], [4, 3], [5, 4]]

     0  1  2
       | /
        3
        |
        4
        |
        5 

Output: [3, 4]
Note:

According to the definition of tree on Wikipedia: “a tree is an undirected graph in which any two vertices are connected by exactly one path. In other words, any connected graph without simple cycles is a tree.”
The height of a rooted tree is the number of edges on the longest downward path between the root and a leaf.

在無(wú)向圖的生成樹(shù)中,我們可以指定任何一個(gè)節(jié)點(diǎn)為這棵樹(shù)的根節(jié)點(diǎn)。現(xiàn)在要求在這樣一棵生成樹(shù)中,找到生成樹(shù)的高度最低的所有根節(jié)點(diǎn)。

其實(shí),決定一棵樹(shù)的高度往往是樹(shù)中的最長(zhǎng)路徑,只有我們選擇最長(zhǎng)路徑的中間點(diǎn)才能夠盡可能減少樹(shù)的高度。那么我們?nèi)绾握业剿械闹虚g節(jié)點(diǎn)呢?

當(dāng)出發(fā)點(diǎn)只有兩個(gè)時(shí),我們知道中間節(jié)點(diǎn)就是從出發(fā)點(diǎn)同時(shí)出發(fā),當(dāng)二者相遇或者二者只間隔一個(gè)位置是所在的點(diǎn)就是兩個(gè)出發(fā)點(diǎn)的重點(diǎn)。那么多個(gè)出發(fā)點(diǎn)也是同樣的道理,每個(gè)人從各個(gè)出發(fā)點(diǎn)出發(fā),最終相遇的點(diǎn)就是我們所要找的中間點(diǎn)。

這題的思路有些類似于拓?fù)渑判?,每次我們都?huì)去除所有入度為0的點(diǎn),因?yàn)檫@些點(diǎn)肯定是葉節(jié)點(diǎn)。然后不停的往中間走,直到剩下最后一個(gè)葉節(jié)點(diǎn)或是最后兩個(gè)葉節(jié)點(diǎn)。

  0
  |
  1
 / 
2   3

這個(gè)圖中刪除所有入度為0的點(diǎn)就只剩下1,因此我們知道1一定就是我們所要求的根節(jié)點(diǎn)

思路一:圖論

這一種解法著重強(qiáng)調(diào)了利用圖論中的數(shù)據(jù)結(jié)構(gòu)來(lái)解決問(wèn)題。這里我們采用圖論中的鄰接表來(lái)存儲(chǔ)圖中的點(diǎn)和邊。然后利用鄰接表的相關(guān)屬性來(lái)判斷當(dāng)前節(jié)點(diǎn)是否是葉節(jié)點(diǎn)。

    public List findMinHeightTrees(int n, int[][] edges) {
        if(n==1) return Collections.singletonList(0);
        //初始化鄰接表
        List> adj = new ArrayList>();
        for(int i = 0 ; i());
        }
        for(int[] edge : edges) {
            adj.get(edge[0]).add(edge[1]);
            adj.get(edge[1]).add(edge[0]);
        }
        
        List leaves = new ArrayList();
        for(int i = 0 ; i 2) {
            n -= leaves.size();
            List newLeaves = new ArrayList<>();
            for (int i : leaves) {
                int j = adj.get(i).iterator().next();
                adj.get(j).remove(i);
                if (adj.get(j).size() == 1) newLeaves.add(j);
            }
            leaves = newLeaves;
        }
        return leaves;
    }
思路二:簡(jiǎn)化數(shù)據(jù)結(jié)構(gòu)

這里使用degree數(shù)組存儲(chǔ)每個(gè)頂點(diǎn)的度數(shù),即連接的變數(shù)。度數(shù)為一的頂點(diǎn)就是葉節(jié)點(diǎn)。再用connected存儲(chǔ)每個(gè)頂點(diǎn)所連接的所有邊的異或值。這里使用異或的原因是對(duì)同一個(gè)值進(jìn)行兩次異或即可以回到最初值。

    public List findMinHeightTrees2(int n, int[][] edges) {
        if(n==1) return Collections.singletonList(0);
        int[] connected = new int[n];
        int[] degree = new int[n];
        
        for(int[] edge : edges) {
            int v1 = edge[0];
            int v2 = edge[1];
            connected[v1] ^= v2;
            connected[v2] ^= v1;
            
            degree[v1]++;
            degree[v2]++;
        }
        
        LinkedList queue = new LinkedList();
        for(int i = 0 ; i 2 && !queue.isEmpty()) {
            int size = queue.size();
            for(int i = 0 ; i result = new ArrayList();
        result.addAll(queue);
        return result;
    }


想要了解更多開(kāi)發(fā)技術(shù),面試教程以及互聯(lián)網(wǎng)公司內(nèi)推,歡迎關(guān)注我的微信公眾號(hào)!將會(huì)不定期的發(fā)放福利哦~

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://specialneedsforspecialkids.com/yun/72303.html

相關(guān)文章

  • Leetcode 310. Minimum Height Trees

    摘要:可以從頭邊同時(shí)進(jìn)行,查看葉子節(jié)點(diǎn)并加入到葉子節(jié)點(diǎn)鏈表遍歷一遍后,葉子節(jié)點(diǎn)鏈表為。將葉子節(jié)點(diǎn)保存下來(lái)。這個(gè)時(shí)候就會(huì)有第二層葉子節(jié)點(diǎn)那些在列表當(dāng)中為的點(diǎn),用同樣的方法進(jìn)行剝除。最后留在葉子節(jié)點(diǎn)里面的點(diǎn)即可以為根。 題目: For a undirected graph with tree characteristics, we can choose any node as the root...

    xuxueli 評(píng)論0 收藏0
  • Minimum Height Trees

    摘要:題目鏈接圖的題,和差不多。來(lái)解,每次放入只有一個(gè)的現(xiàn)在的。然后直到只剩最上面一層。注意考慮單獨(dú)的和別的不相連。比如這種情況,就有三個(gè)點(diǎn)都不和別的相連。還有的時(shí)候就要返回。 Minimum Height Trees 題目鏈接:https://leetcode.com/problems... 圖的題,和course schedule差不多。bfs來(lái)解,每次放入只有一個(gè)edge的node(現(xiàn)...

    joyqi 評(píng)論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 評(píng)論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 評(píng)論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 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<