<p>A tree is an undirected graph in which any two vertices are connected by&nbsp;<i>exactly</i>&nbsp;one path. In other words, any connected graph without simple cycles is a tree.</p>

<p>Given a tree of <code>n</code> nodes&nbsp;labelled from <code>0</code> to <code>n - 1</code>, and an array of&nbsp;<code>n - 1</code>&nbsp;<code>edges</code> where <code>edges[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> indicates that there is an undirected edge between the two nodes&nbsp;<code>a<sub>i</sub></code> and&nbsp;<code>b<sub>i</sub></code> in the tree,&nbsp;you can choose any node of the tree as the root. When you select a node <code>x</code> as the root, the result tree has height <code>h</code>. Among all possible rooted trees, those with minimum height (i.e. <code>min(h)</code>)&nbsp; are called <strong>minimum height trees</strong> (MHTs).</p>

<p>Return <em>a list of all <strong>MHTs&#39;</strong> root labels</em>.&nbsp;You can return the answer in <strong>any order</strong>.</p>

<p>The <strong>height</strong> of a rooted tree is the number of edges on the longest downward path between the root and a leaf.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/e1.jpg" style="width: 800px; height: 213px;" />
<pre>
<strong>Input:</strong> n = 4, edges = [[1,0],[1,2],[1,3]]
<strong>Output:</strong> [1]
<strong>Explanation:</strong> As shown, the height of the tree is 1 when the root is the node with label 1 which is the only MHT.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/09/01/e2.jpg" style="width: 800px; height: 321px;" />
<pre>
<strong>Input:</strong> n = 6, edges = [[3,0],[3,1],[3,2],[3,4],[5,4]]
<strong>Output:</strong> [3,4]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 2 * 10<sup>4</sup></code></li>
	<li><code>edges.length == n - 1</code></li>
	<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt; n</code></li>
	<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
	<li>All the pairs <code>(a<sub>i</sub>, b<sub>i</sub>)</code> are distinct.</li>
	<li>The given input is <strong>guaranteed</strong> to be a tree and there will be <strong>no repeated</strong> edges.</li>
</ul>

import heapq
class Solution:
    def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]:
        graph=defaultdict(list)
        degree={}
        for i in range(n):
            degree[i]=0

        
        for edge in edges:
            degree[edge[0]]+=1
            degree[edge[1]]+=1
            graph[edge[0]].append(edge[1])
            graph[edge[1]].append(edge[0])

        
        queue=[]
        visited=set()

        print(degree,graph,"hey")
        for ele in degree.keys():
            if degree[ele]==1 or degree[ele]==0:
                queue.append(ele)
                degree[ele]=0
                visited.add(ele)
        print(queue)

        nodesrem=n
        
        while nodesrem>2:
            nodesrem-=len(queue)
            n=len(queue)
            # print(queue,degree,"hello")
            for i in range(n):
                ele=queue.pop(0)
                for nei in graph[ele]:
                        if degree[nei]-1==1:
                            degree[nei]-=1
                            queue.append(nei)
                        else:
                            degree[nei]-=1
                # print(degree,"count")
        print(degree)
        return queue

        # visited
        # queue=[0]
        # while len(queue)!=0:
        # return [2,3]
            
        # def heights(root,n):
        #     visited=[False for _ in range(n)]
        #     queue=deque([root])
        #     h=0
        #     while len(queue)!=0:
        #         n=len(queue)
        #         h+=1
                
                
        #         for i in range(n):
        #             ele=queue.popleft()
        #             visited[ele]=True
        #             for nodes in graph[ele]:
        #                 if visited[nodes]!=True:
        #                     queue.append(nodes)
        #     return h
                    


        # heap=[]
        # heapq.heapify(heap)
        # for i in range(n):
        #     height[i]=height.get(i,0)+heights(i,n)
        #     heapq.heappush(heap,(height[i],i))
        #     # 
        # print(height)
        # print(heap)
        # ans=[]
        # popped=heapq.heappop(heap)
        # prev=popped[0]
        # ans.append(popped[1])
        # while True and len(heap)!=0:
        #     popped=heapq.heappop(heap)

        #     if(prev==popped[0]):
        #         ans.append(popped[1])
        #     else:
        #         break
        #     # prev=popped[0]
        # print(ans)
        # return ans


        
        
29200