id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
309
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll...
3
{ "code": "// Utility function for hashing pairs in unordered_map\nstruct hash_pair {\n template <class T1, class T2>\n size_t operator() (const pair<T1, T2>& p) const {\n auto hash1 = hash<T1>{}(p.first);\n auto hash2 = hash<T2>{}(p.second);\n return hash1 ^ hash2;\n }\n};\nstatic int s...
309
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll...
3
{ "code": "class Solution {\npublic:\nint dfs(unordered_map<int,unordered_map<bool,int>> &dp, int pos, vector<int>&prices, bool buying){ \n if(pos >= prices.size()) return 0;\n if(dp[pos][buying]) return dp[pos][buying];\n int cooldown = dfs(dp,pos+1,prices,buying);\n if(buying){\n int buy = dfs...
309
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll...
3
{ "code": "class Solution {\npublic:\n\n unordered_map<int, unordered_map<bool, int>> dp;\n vector<int> p;\n\n int dfs(int i, bool buy) {\n if (i >= p.size()) return 0;\n if (dp.contains(i) && dp[i].contains(buy)) return dp[i][buy];\n\n int cd = dfs(i + 1, buy);\n if (buy) {\n ...
309
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll...
3
{ "code": "class Solution {\npublic:\n std::unordered_map<int, std::unordered_map<bool, int>> dp;\n int maxProfit(vector<int>& prices) {\n int res = maxProfit(prices, 0, true);\n return res;\n }\n // 0: can buy 1: can sell 2: cooldown;\n int maxProfit(const std::vector<int>& prices, int d...
309
<p>You are given an array <code>prices</code> where <code>prices[i]</code> is the price of a given stock on the <code>i<sup>th</sup></code> day.</p> <p>Find the maximum profit you can achieve. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times) with the foll...
3
{ "code": "class Solution {\npublic:\n\n int dfs(vector<int>& prices, unordered_map<string,int>& dp, int i, bool buying)\n {\n if(i>=prices.size())\n return 0;\n string key = '#' + to_string(i) + to_string(buying);\n cout<<key<<endl;\n if (dp.count(key)) {\n ret...
310
<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...
0
{ "code": "#define MAX(x,y) x < y ? y : x\n\nstruct Edge {\n int to;\n int next;\n int depth;\n};\n\nclass Solution {\npublic:\n Edge edges[40000];\n int head[20000]{};\n int edgeCount = 0;\n\n\n void addEdge(int x, int y) {\n edges[edgeCount] = {y, head[x], -1};\n head[x] = edgeCou...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (edges.empty()) return vector<int>{0};\n if (edges.size() == 1) return edges[0];\n\n int *heighborsSum = new int[n];\n int *heighborsCount = new int[n];\n for (int i = ...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n // count the degree\n vector<int> counts(n, 0);\n // a xor a = a, a xor b xor a = b\n // if the degree is 1 (only one edge), link[i] is the node on the other side of the edge\n ...
310
<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...
0
{ "code": "#include <vector>\n#include <queue>\n#include <algorithm>\n\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<int> counts(n, 0);\n vector<int> links(n, 0);\n \n for (auto& edge : edges) {\n ...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (n == 1) return {0};\n // <sum of next nodes, cnt of next nodes>\n vector<pair<int, int>> graph(n); \n for (auto& e : edges) {\n graph[e[0]].first += e[1];\n ...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (n == 1) return {0};\n // <sum of next nodes, cnt of next nodes>\n vector<pair<int, int>> graph(n); \n for (auto& e : edges) {\n graph[e[0]].first += e[1];\n ...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (n == 1) return {0};\n vector<int> gp[n];\n int in[n]; memset(in, 0, sizeof(in));\n\n for (vector<int> &v: edges) {\n int a = v[0], b = v[1];\n gp[a].emp...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n int sz = edges.size();\n vector<int> adj[n];\n int ct[n];\n for(int i=0; i<n; i++) ct[i] = 0;\n for(int i=0; i<sz; i++)\n {\n int a = edges[i][0];\n ...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n==1) return {0};\n vector<int> ans;\n queue<int> q;\n vector<int> adj[n+1];\n int deg[n+1];\n for(int i =0; i<n;i++)\n {\n deg[i]=0;\n ...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) \n {\n if(n==1) return {0};\n\n vector<int>count(n);\n vector<int> edg[n];\n for(auto &x:edges)\n {\n int u = x[0], v = x[1];\n count[u]++, count[v]++...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n == 1)\n return {0};\n int size = edges.size();\n vector<int> indegree(n);\n vector<int> adj[n];\n for(int i = 0; i < edges.size(); i++){\n adj[e...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n==0) return {};\n if(n==1) return {0};\n vector<int> adj[n];\n vector<int> indeg(n,0);\n for(int i=0; i<edges.size(); i++){\n adj[edges[i][1]].push_back(ed...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n==1) return {0};\n vector<int> adj[n];\n vector<int> deg(n,0);\n vector<int> ans;\n queue<int> q;\n for(int i=0;i<edges.size();i++){\n adj[edges[i][0...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n int degree[n];\n bool visited[n];\n int node;\n int largest_path_len = 0;\n vector<int> adj_list[n];\n vector<int> output;\n memset(degree , 0 , sizeof(int) ...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n int degree[n];\n bool visited[n];\n int node;\n int largest_path_len = 0;\n vector<int> adj_list[n];\n vector<int> output;\n\n\n // if their is no edge in tr...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& v) {\n if(n==1)return {0};\n vector<int>g[n+1];\n vector<int>vis(n+1,0);\n vector<int>ind(n+1,0);\n vector<int>res;\n for(int i=0;i<v.size();i++)\n {\n int x...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& e) {\n vector<int> deg(n,0);\n vector<int> adj[n];\n for(int i=0;i<e.size();i++){\n adj[e[i][0]].push_back(e[i][1]);\n adj[e[i][1]].push_back(e[i][0]);\n deg[e[i][...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<int> ans;\n if (n == 1) {\n ans.push_back(0);\n return ans;\n } else {\n vector<int> in_degree(n,0);\n vector<int> v[n];\n for ...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<int>adj[n];\n vector<int>indegree(n);\n for(int i=0;i<edges.size();i++){\n int u=edges[i][0];\n int v=edges[i][1];\n adj[u].push_back(v);\n ...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n == 1) return vector<int>(1, 0);\n vector<int> adjList[n];\n vector<int> indegree(n);\n for(auto &v : edges) {\n int u1 = v[0], u2 = v[1];\n adjList[u1]...
310
<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...
0
{ "code": "class Solution {\npublic:\n \n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n \n int u, v, h,sz;\n int mini = 1e9;\n vector<int>ht(n);\n \n vector<int>adj[n];\n \n vector<int>vis(n, 0);\n vector<int>vec;\n ve...
310
<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...
0
{ "code": "class Solution {\npublic:\n#define ll int \n#define pb push_back\n\nll cal_dia(vector<ll>adj[],vector<ll>&deg,ll n,vector<ll>&depth){\n vector<ll> vis(n,0);\n queue<ll>q;\n ll dia=0;\n // calculate the degree of each vertex\n for(ll i=0;i<n; i++){\n if(deg[i]==1) q.push(i);\n }\n\n ...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<int> res;\n if(n <= 2){\n for(int i = 0; i < n; ++i) res.push_back(i);\n return res;\n }\n \n vector<int> g[n];\n vector<int> deg(n);\n for(auto...
310
<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...
0
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n==1) return {0};\n vector<vector<int>>adj(n);\n for(int i=0 ; i<edges.size() ; i++){\n adj[edges[i][1]].push_back(edges[i][0]);\n adj[edges[i][0]].push_back(ed...
310
<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...
1
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<int> deg(n, 0);\n vector<vector<int>> adj(n, vector<int>());\n for(auto &it: edges){\n deg[it[0]]++;\n deg[it[1]]++;\n adj[it[0]].push_back(it[1]...
310
<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...
1
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<int> deg(n, 0);\n vector<vector<int>> adj(n, vector<int>());\n for(auto &it: edges){\n deg[it[0]]++;\n deg[it[1]]++;\n adj[it[0]].push_back(it[1]...
310
<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...
1
{ "code": "class Solution {\nprivate:\n vector<vector<int>> g;\n vector<int> dis;\n vector<int> par;\n \n void dfs(int node, int parent, int d){\n dis[node] = d;\n par[node] = parent;\n for (int ngbh: g[node]){\n if (ngbh == parent) continue;\n if (dis[ngbh] !...
310
<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...
1
{ "code": "class Solution {\npublic:\n\n void DFS(vector<int>& hights, vector<vector<int>>& adj_list, int v, int p) {\n for (auto u : adj_list[v]) {\n if (u == p) {\n continue;\n }\n DFS(hights, adj_list, u, v);\n }\n for (auto u : adj_list[v]) {...
310
<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...
1
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (n ==1 ) {\n return {0};\n }\n vector<int>v[n];\n vector<int>degree(n);\n for(auto u : edges) {\n degree[u[0]]++;\n degree[u[1]]++;\n ...
310
<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...
1
{ "code": "struct OutNodeWithDepth {\n int node = 0;\n int depth = 0;\n};\n\nstruct NodeData {\n std::vector<OutNodeWithDepth> out_nodes;\n int depth = 0;\n};\n\nclass Solution {\npublic:\n vector<int> findMinHeightTrees(const int n,\n const vector<vector<int>>& edges)...
310
<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...
1
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n\n if(n==1) {\n return {0};\n }\n\n vector<int> roots;\n vector<int> adjList[n];\n vector<int> degree (n, 0);\n for(auto edge : edges) {\n adjL...
310
<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...
1
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<int> ans;\n if (n == 1) {\n ans.push_back(0);\n return ans;\n } else {\n vector<int> in_degree(n,0);\n vector<int> v[n];\n for ...
310
<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...
1
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (n == 1)\n return vector<int>(1, 0);\n vector<vector<int>> adj(n, vector<int>());\n vector<int> indegree(n, 0);\n for (vector<int>& edge: edges) {\n adj[...
310
<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...
1
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (n == 1) return {0}; // Special case for a single node\n \n // Build the adjacency list\n vector<vector<int>> adj(n);\n for (const auto& edge : edges) {\n a...
310
<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...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> g;\n vector<int> d1, d2, p1, p2, up; // p1和p2表示d1和d2对应的路径上的下一个点\n\n void dfs1(int u, int father) {\n // 对于每个u,枚举其所有邻点v\n for (int v: g[u]) {\n if (v == father) continue;\n dfs1(v, u);\n int d = d1[v] + 1...
310
<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...
1
{ "code": "class Solution {\npublic:\n vector<vector<int>> g;\n vector<int> d1, d2, p1, p2, up;\n\n void dfs1(int u, int father) {\n for (auto& v: g[u]) {\n if (v == father) continue;\n dfs1(v, u);\n int d = d1[v] + 1;\n if (d >= d1[u]) {\n d2...
310
<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...
2
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (n == 1) return {0};\n vector <int> ans;\n queue <int> q;\n vector <vector <int>> g(n);\n vector <int> cnt(n, 0); // to store the degree of every node\n for (vec...
310
<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...
2
{ "code": "class Solution {\npublic:\n vector<vector<int>> adj;\n vector<pair<int, int>> child;\n vector<pair<int, int>> second_child;\n vector<pair<int, int>> maxlen1;\n vector<int> maxlen2;\n\n int compute_child(int cur, int prev=-1) {\n int len = 0;\n for (int neighbor : adj[cur]) {...
310
<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...
2
{ "code": "class Solution {\npublic:\n \n vector<int> findMinHeightTrees(int n, vector<vector<int>>& pre) {\n vector<int>res;\n if(n<2){\n for(int i=0;i<n;i++) res.push_back(i);\n \n return res;\n }\n vector<vector<int>>adj(n);\n vector<int>ind...
310
<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...
2
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n == 1) return {0};\n vector<vector<int>> graph(n);\n vector<int> degree(n, 0);\n \n for(auto edge: edges){\n int u = edge[0], v = edge[1];\n gra...
310
<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...
2
{ "code": "class Solution {\npublic:\n int maxDepth;\n int farthestNode;\n void dfs(int s, vector<bool> &visited, vector< vector<int> > &adj, int currDepth, vector<int> &path) {\n visited[s] = true;\n if(maxDepth < currDepth) {\n maxDepth = currDepth;\n farthestNode = s;\n...
310
<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...
2
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n == 1) {\n return {0};\n }\n\n vector<vector<int>> adj_list(n, vector<int>());\n vector<int> indegree(n, 0);\n for(auto edge: edges) {\n adj_list...
310
<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...
2
{ "code": "struct OutNodeWithDepth {\n int node = 0;\n int depth = 0;\n};\n\nstruct NodeData {\n std::vector<OutNodeWithDepth> out_nodes;\n int depth = 0;\n};\n\nclass Solution {\npublic:\n vector<int> findMinHeightTrees(const size_t n,\n const vector<vector<int>>& edg...
310
<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...
2
{ "code": "class Solution {\npublic:\n \n vector<vector<int>> g;\n \n void dfs(int level, int node, int par, vector<int> &dist, vector<int> &parent) {\n dist[node] = level;\n parent[node]=par;\n for(auto x: g[node]){\n if(x!=par){\n dfs(level+1, x, node, dist...
310
<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...
2
{ "code": "struct OutNodeWithDepth {\n int node = 0;\n int depth = 0;\n};\n\nstruct NodeData {\n std::vector<OutNodeWithDepth> out_nodes;\n int depth = 0;\n};\n\nclass Solution {\npublic:\n vector<int> findMinHeightTrees(const size_t n,\n const vector<vector<int>>& edg...
310
<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...
2
{ "code": "struct OutNodeWithDepth {\n int node = 0;\n int depth = 0;\n};\n\nstruct NodeData {\n std::vector<OutNodeWithDepth> out_nodes;\n int depth = 0;\n};\n\nclass Solution {\npublic:\n vector<int> findMinHeightTrees(const size_t n,\n const vector<vector<int>>& edg...
310
<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...
2
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<int>indeg(n,0);\n unordered_map<int,vector<int>>mp;\n vector<int>ans;\n for(int i=0;i<edges.size();i++){\n indeg[edges[i][0]]++;\n indeg[edges[i][1]]...
310
<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...
2
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n unordered_map<int, vector<int>> adj;\n\n vector<int> indegree(n);\n if(n == 1){\n return {0};\n }\n\n for(auto &edge : edges)\n {\n int u = ed...
310
<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...
2
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n \n \n //1) create adjacency List \n set<int> MHTRoots;\n vector<int> height(n,0);\n \n vector<vector<int>> adjList(n);\n \n for (int e=0;e<n-1;...
310
<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...
2
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n \n \n //1) create adjacency List \n set<int> MHTRoots;\n vector<int> height(n,0);\n \n vector<vector<int>> adjList(n);\n \n for (int e=0;e<n-1;...
310
<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...
2
{ "code": "class Solution {\npublic:\n \n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n\n int degree[n];\n\n if(n == 1){\n return {0};\n }\n\n vector<vector<int>> graph;\n\n for(int i=0;i<n;i++){\n degree[i] = 0;\n\n vector<int> temp;\n ...
310
<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...
2
{ "code": "class Solution {\npublic:\n vector<int> ans;\n void bfs(vector<int> &vis,unordered_map<int,vector<int>> &graph,vector<int> &inOrder){\n queue<int> q;\n for(int i=0;i<inOrder.size();i++){\n if(inOrder[i] == 1){\n q.push(i);\n vis[i] = 1;\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n void DFS(int src, int parent, vector<bool>& visited, vector<vector<int> >& tree, vector<pair<int, int> >& dp, vector<int>& par){\n visited[src] = true;\n par[src] = parent;\n for(int i=0;i<tree[src].size();i++){\n int neighbor = tree[src][i];\n...
310
<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...
3
{ "code": "class Solution {\n vector<vector<int>> graph ;\n int farthestDist = 0 , farthestNode = -1 ;\n vector<int> ans ;\n\n\n void findFarthestNode(int root, int par, int dist) {\n if(dist > farthestDist){\n farthestDist = dist ;\n farthestNode = root ;\n }\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (n == 1) {\n return {0};\n }\n unordered_map<int, vector<int>> g;\n vector<int> degree(n, 0);\n for (auto &edge: edges) {\n g[edge[0]].push_back(e...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<vector<int>> adj(n);\n for(auto i:edges)\n {\n int u = i[0] , v = i[1];\n adj[u].push_back(v);\n adj[v].push_back(u);\n }\n vector<...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n // trivial edge cases\n if(n == 1){\n return {0};\n } \n else if(n == 2){\n return {0, 1};\n }\n\n // nodes remaining in the graph after rem...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n \n // 从任意 Vertex 出发, 找到 longest path\n std::unordered_map<int, std::vector<int>> neighbors;\n for (auto& edge : edges) {\n neighbors[edge[0]].push_back(edge[1]);\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<vector<int>> graph(n);\n\n for (auto it: edges) {\n graph[it[0]].push_back(it[1]);\n graph[it[1]].push_back(it[0]);\n }\n\n vector<bool> seen(n , fal...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n \n // 从任意 Vertex 出发, 找到 longest path\n std::unordered_map<int, std::vector<int>> neighbors;\n for (auto& edge : edges) {\n neighbors[edge[0]].push_back(edge[1]);\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n unordered_map<int,vector<int>> adj;\n vector<int> indegree(n,0);\n\n for(auto i:edges){\n int u=i[0];\n int v=i[1];\n\n adj[u].push_back(v);\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n==1) return {0};\n unordered_map<int, list<int>> adj;\n vector<int> indegree(n, 0);\n for (int i = 0; i < edges.size(); i++) {\n int u = edges[i][0];\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (n == 1) return {0};\n\n unordered_map<int, vector<int>> graph;\n vector<int> buffer(n, 0);\n\n for (auto e : edges) {\n graph[e[0]].push_back(e[1]);\n g...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n == 1)\n return {0};\n \n vector<int> result;\n vector<int> indegree(n);\n map<int, vector<int>> adj;\n \n for(vector<int> vec:edges) {\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n==1){\n return {0};\n }\n else if(n==2){\n return {0,1};\n }\n\n vector<int> ret;\n vector<vector<int>> st(n);\n vector<int> deg(n,...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n==1){\n return {0};\n }\n if(n==2){\n return {0,1};\n }\n vector<vector<int>> ed(n);\n vector<int> deg(n);\n for(int i = 0; i < (in...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (n == 1) return {0}; // For one node, return the only node 0.\n if (n == 2) return {0, 1}; // For two nodes, return both nodes.\n \n // Build the adjacency list for the grap...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<vector<int>> g(n);\n vector<int> indeg(n,0);\n \n for(int i=0;i<edges.size();i++){\n g[edges[i][0]].push_back(edges[i][1]);\n g[edges[i][1]].push_bac...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n==1)return {0};\n vector<set<int>> g(n);\n for(auto e: edges){\n g[e[0]].insert(e[1]);\n g[e[1]].insert(e[0]);\n }\n queue<int> q;\n vecto...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if( n==1 ) return {0};\n vector< set<int> > g(n, set<int>());\n vector<int> cnt(n, 0);\n vector<int> ans;\n for( auto edge: edges ){\n int n1 = edge[0];\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\nvector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\nunordered_map<int,vector<int>> adj;\n\nvector<int> degree(n,0);\nif(edges.size() == 1)\n{\n int u = edges[0][0];\n int v = edges[0][1];\n vector<int> ans;\n ans.push_back(u);\n ans.push_back(v);\n...
310
<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...
3
{ "code": "class Solution {\npublic:\n struct Node\n {\n int degree;\n int idx;\n vector<Node*> neighbors;\n Node(int idx): idx(idx), degree(0) {};\n };\n vector<Node> m_Nodes;\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (edges.size() ==...
310
<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...
3
{ "code": "class Solution {\npublic:\n void bfs(int node, unordered_map<int, vector<int>> &adj, unordered_map<int, bool> &visited){\n queue<int> q; \n q.push(node); \n while(!q.empty()){\n node = q.front();\n visited[node] = true;\n cout << node;\...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<set<int>>adj(n);\n if(n==1){\n return {0};\n }\n for(auto edge: edges){\n adj[edge[0]].insert(edge[1]);\n adj[edge[1]].insert(edge[0]);\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n\n if(n==1)\n return {0};\n\n unordered_map<int, vector<int>> adj;\n vector<int> degrees(n,0);\n\n for(auto edge:edges)\n {\n adj[edge[0]].push_back(e...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n == 1) return {0};\n vector<int> leafs = {};\n unordered_map<int,vector<int>> umap;\n vector<int> grade(n,0);\n\n for(vector<int> edge:edges){\n umap[edge[0...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) \n {\n if(n==1)\n {\n return {0};\n }\n vector<int>ans;\n unordered_map<int,list<int>>adj;\n vector<int>digree(n,0);\n for(auto it:edges)\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n==1)return {0};\n unordered_map<int,int>indeg;\n unordered_map<int,vector<int>>adj;\n for(auto it:edges){\n adj[it[0]].push_back(it[1]);\n adj[it[1]].pu...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if (n == 2) return edges[0];\n if (n == 1) return {0};\n unordered_map<int,vector<int>> graph;\n unordered_map<int, int> degree;\n // Count for frequencies of nodes to fin...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(n==1) return {0};\n unordered_map<int,vector<int>>adj;\n unordered_map<int,int>degree;\n for(auto edge:edges) {\n adj[edge[0]].push_back(edge[1]);\n adj[...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n int tnode = n;\n vector<vector<int>> graph(n);\n vector<int> deg(n, 0);\n for(auto edge : edges){\n int f = edge[0];\n int s = edge[1];\n graph[f...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n unordered_map<int, int> inDegree;\n for (int i = 0; i < n; i++) {\n inDegree[i] = 0;\n }\n\n unordered_map<int, vector<int>> um;\n for (auto edge : edges) {\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n\n if(n==1){\n vector<int> v;\n v.push_back(0);\n return v ;\n }\n\n map<int,vector<int>> mp;\n\n for(auto it: edges){\n mp[it[0]].push...
310
<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...
3
{ "code": "class Solution {\npublic:\n int height(int u,vector<bool>&visited,vector<vector<int>>&adj,int &ans)\n {\n vector<int>v;\n visited[u]=true;\n for(int i=0;i<adj[u].size();i++)\n {\n if(!visited[adj[u][i]])\n {\n int h = height(adj[u][i],v...
310
<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...
3
{ "code": "class Solution {\npublic:\n int dfs(vector<vector<int> > &graph, vector<bool> &visited, int i, int &result, int& node){\n\n visited[i] = true;\n int max1 = -1, max2 = -1, len;\n for( auto nbr : graph[i] ){\n if( !visited[nbr] ){\n len = 1+dfs(graph, visited...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n ios::sync_with_stdio(0);\n cin.tie(0); cout.tie(0);\n\n if (n == 1) return {0};\n\n unordered_set<int> adj[20001];\n for (const auto &edge : edges) {\n adj[edge...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n ios::sync_with_stdio(0);\n cin.tie(0); cout.tie(0);\n\n if (n == 1) return {0};\n\n unordered_set<int> adj[20001];\n for (const auto &edge : edges) {\n adj[edge...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n vector<vector<int>> adj(n);\n for (auto e: edges) {\n adj[e[0]].push_back(e[1]);\n adj[e[1]].push_back(e[0]);\n }\n // first find diameter\n int dis[...
310
<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...
3
{ "code": "class Solution {\npublic:\n std::vector<int> findMinHeightTrees(int n, std::vector<std::vector<int>>& edges) {\n std::unordered_map<int, std::vector<int>> adjecencyList;\n std::unordered_map<int, int> degreeMap;\n std::vector<int> empty;\n if(edges.size()<=0){\n em...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n unordered_map<int,vector<int>> adjNodes;\n vector<int> res;\n\n for(vector<int> e:edges){\n adjNodes[e[0]].push_back(e[1]);\n adjNodes[e[1]].push_back(e[0]);\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n \n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n if(edges.size()==0 && n==1) return{0};\n unordered_map<int, list<int>> adjList;\n unordered_map<int, int> degree;\n for(auto e: edges){\n int u=e[0];\n ...
310
<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...
3
{ "code": "class Solution {\npublic:\n\n bool isLeaf(int x, const vector<set<int>>& graph) {\n return graph[x].size() == 1;\n }\n\n int removeLeaf(int leaf, vector<set<int>>& graph) {\n int neig = *graph[leaf].begin();\n graph[leaf].clear();\n graph[neig].erase(leaf);\n ret...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n unordered_map<int, vector<int>> graph;\n for (const vector<int>& v : edges) {\n graph[v[0]].push_back(v[1]);\n graph[v[1]].push_back(v[0]);\n }\n vector<boo...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n unordered_map<int, vector<int>> parentMap;\n unordered_map<int, int> childCountMap;\n\n\n for(int i = 0; i < n; i++) {\n parentMap.insert({i, {}});\n childCountMap...
310
<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...
3
{ "code": "class Solution {\npublic:\n vector<int> findMinHeightTrees(int n, vector<vector<int>>& edges) {\n unordered_set<int> adj[n];\n for (auto it: edges) {\n int u = it[0], v = it[1];\n\n adj[u].insert(v);\n adj[v].insert(u);\n }\n\n queue<int> leaf...