Minimum Height Trees - Leetcode 310 - Python

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🧑‍💼 LinkedIn: / navdeep-singh-3aaa14161
🐦 Twitter: / neetcode1
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
Problem Link: leetcode.com/problems/minimum...
0:00 - Read the problem
1:41 - Drawing Explanation 1
11:23 - Drawing Explanation 2
15:37 - Coding Explanation
leetcode 310
#neetcode #leetcode #python

Пікірлер: 43

  • @yang5843
    @yang58433 ай бұрын

    Whenever I struggle with daily Leetcode problems, I turn to Neetcode

  • @shreehari2589

    @shreehari2589

    3 ай бұрын

    Ok

  • @alexeyfv
    @alexeyfv3 ай бұрын

    I was only able to implement a brute force solution, but only 67 out of 71 test cases passed (got time limit exception). 😁Thanks for the video.

  • @bhuvan9956
    @bhuvan99563 ай бұрын

    Thanks for this. Please do daily LCs and Contest please.

  • @hamirmahal
    @hamirmahal3 ай бұрын

    Thanks for putting this up!

  • @Droid261
    @Droid2613 ай бұрын

    Thank you, I needed this

  • @jand2861
    @jand28613 ай бұрын

    really cool stuff man, thanks for the content

  • @MP-ny3ep
    @MP-ny3ep3 ай бұрын

    Thank you so much for the daily. Really helps a lot.

  • @buckboot
    @buckboot3 ай бұрын

    Thanks for this

  • Ай бұрын

    Perhaps slightly simpler (more familiar BFS) impl where we can get rid of the layer traversal technique. At the end of the while loop, all root will have the highest layer value (since they're the last layer) 😃 // Initialize layers with -1, except leaves layers are 0. int max_layer = 0; while (!q.empty()) { int u = q.front(); q.pop(); link[u]--; for (int v : adj[u]) { link[v]--; // Here, one of u's neighbors v just turned into a leaf node after u is removed. if (link[v] == 1 && layers[v] == -1) { layers[v] = layers[u] + 1; max_layer = max(max_layer, layers[v]); q.push(v); } } } vector res; for (int i = 0; i if (layers[i] == max_layer) { res.push_back(i); } }

  • @_N_E_E_R_A_J_
    @_N_E_E_R_A_J_3 ай бұрын

    I solved it using re-rooting. But this "Removing leaf nodes" solution is very amazing. I didn't thought of that. Thank you so much!💖

  • @srprawinraja4261
    @srprawinraja42613 ай бұрын

    Thanks 😊

  • @user-pv4xn3sg7j
    @user-pv4xn3sg7j3 ай бұрын

    Missed you buddy 😭

  • @tekfoonlim4745
    @tekfoonlim47453 ай бұрын

    Hey Navdeep how are you? I love your leetcode explanations and your solutions! Gives me motivation to do more leetcode

  • @impatientgaming9868
    @impatientgaming98683 ай бұрын

    Good one

  • @omarr993
    @omarr9933 ай бұрын

    edging to this rn

  • @chaitanyasharma6270
    @chaitanyasharma62703 ай бұрын

    Kahn's algorithm as intuition for this question. topsort(remove) where indegree ==1

  • @singletmat5172
    @singletmat51723 ай бұрын

    I liked the note about the length and how other languages would calculate the length. With so much python sugar, we forget how a similar code would break in Java or C.

  • @gary1614
    @gary16143 ай бұрын

    Great video as always! One thing that's worth mentioning is that if n

  • @KaranBulani

    @KaranBulani

    3 ай бұрын

    was thinking same

  • @bellxlilies9913

    @bellxlilies9913

    3 ай бұрын

    Yes, if the n

  • @sankhadip_roy
    @sankhadip_roy3 ай бұрын

    code: class Solution: def findMinHeightTrees(self, n: int, edges: List[List[int]]) -> List[int]: if n==1: return [0] adj = defaultdict(list) for n1,n2 in edges: adj[n1].append(n2) adj[n2].append(n1) edgecount = {} leaves = deque() for s, nei in adj.items(): if len(nei)==1: leaves.append(s) edgecount[s]=len(nei) while leaves: if n

  • @phanthe3471
    @phanthe34713 ай бұрын

    it takes me over 15 minutes to be clear the requirement,and until i finished the solution, it takes more than 1 hour.

  • @pixusru

    @pixusru

    3 ай бұрын

    You’re not supposed to come up with requirements and approach on the spot. You must know every possible problem or pattern by heart, because many do, so you’re looking bad compared to them. Sorry, that’s the game.

  • @kumarc4853

    @kumarc4853

    3 ай бұрын

    @@pixusru true, the people who post this arent solving this live and for the first time either. So solve as many as you can and spot patterns, recognize general strategies, things to think about when blocked etc

  • @sophiophile

    @sophiophile

    2 ай бұрын

    ​@@pixusru Tell that to interviewers at FAANG companies :(. I have spent a long time preparing for the code portion of an interview w/ one tomorrow (even though I already have another role essentially secured as a backup). There's always going to be the chance that you just don't spot the optimal solution at that time, no matter how prepped you are. For example, most FAANG would not accept the solution coded up here and would require the second approach he described (since it is O(n) time, O(1) memory- while the first is O(n)/O(n))

  • @yang5843
    @yang58433 ай бұрын

    For the edge case, you could check if length of neighbours is 0 or 1, then it will pass the edge case, without having to create an seperate check for the edge case

  • @Isonich
    @Isonich3 ай бұрын

    No need to check if n==1: return[0], we can return [0] after the while loop and it will work fine.

  • @deathbombs
    @deathbombs3 ай бұрын

    Crazy hard

  • @DBagg-zz4ip
    @DBagg-zz4ip3 ай бұрын

    Okay. At first I thought it was just BFS from the leaves until you have 2 or 1 left. Lots of tests failed. The whole subtracting edges thing went over my head. Second explanation was easy to get though I haven't tried it yet.

  • @EduarteBDO
    @EduarteBDO3 ай бұрын

    This question was super hard, first I tried solving it with DP with memoization, it gave me TLE on test 70 I think

  • @sophiophile
    @sophiophile2 ай бұрын

    Can you do a video using Morris traversal?

  • @georgerussel
    @georgerussel2 ай бұрын

    The brute force solution is still hard to do

  • @sanchitbajaj02
    @sanchitbajaj023 ай бұрын

    Is it ok if after watching an entire video, didn't get the solution considering I only have a basic knowledge of graphs and DFS

  • @ChiragVithlani
    @ChiragVithlani3 ай бұрын

    This problem should be renamed from "Minimum Height Trees" to "Find the root". We can imagine like cutting leaf from all sides equally at same level. What is left is root ( can be two node or one ).

  • @bhavasagar977
    @bhavasagar9773 ай бұрын

    The second approach feels more intuitive for me...

  • @ish90917
    @ish909173 ай бұрын

    Is the time complexity of the code O(n) ?

  • @amitchoraria5737

    @amitchoraria5737

    3 ай бұрын

    i think yeah should be O(n). but just want someone to confirm

  • @juanito1410

    @juanito1410

    3 ай бұрын

    The algorithm here is called khan's algorithm and its TC will be O(V+E) where V is the number of vertices/nodes and E for the count of edges.

  • @jessicakoch2331
    @jessicakoch23313 ай бұрын

    omg i hate graph problems, I hope one day they become less intimidating

  • @YuriiPalchynskyi
    @YuriiPalchynskyi3 ай бұрын

    Proof why leaf nodes can't be a root of min path. Suppose exist a min path, where leaf node is a root A(leaf) - B(end) , the node A has one neighbor N and this neighbor has to be included into the path A-B , from this we can find a root with smaller path N-B

  • @Silquey
    @Silquey3 ай бұрын

    neetcoede