Find Minimum Diameter After Merging Two Trees - Leetcode 3203 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ธ.ค. 2024

ความคิดเห็น • 45

  • @hongyihuang6856
    @hongyihuang6856 4 วันที่ผ่านมา +13

    For those of us who are still here trying to solve a problem like this during this time of the year, kudos to you! Kudos to NeetCode too~

  • @trueinviso1
    @trueinviso1 4 วันที่ผ่านมา +33

    Don't waste too much time on this problem, just watch the solution and learn :D

  • @focminging652
    @focminging652 4 วันที่ผ่านมา +3

    Thank you neetcode you changed my life for the better. Thank you leetcode for the wonderful xmas gift.

  • @jaatharsh
    @jaatharsh 3 วันที่ผ่านมา +1

    ur the best, happy christmas n happy new year buddy

  • @nikenuke
    @nikenuke 3 วันที่ผ่านมา

    Thank you and Happy New Year!

  • @-ArnobBiswas
    @-ArnobBiswas 4 วันที่ผ่านมา +6

    I have watched it like 4 times already and still can't wrap my head around the whole problem 😢

    • @jayjw1
      @jayjw1 4 วันที่ผ่านมา +3

      Rest your mind for the rest of the day, then come back tomorrow and re-watch/re-code, for me that always makes something click if I didn't understand it fully before.

    • @moobs
      @moobs 3 วันที่ผ่านมา

      have you solved the LC easy he mentioned?

    • @tempregex8520
      @tempregex8520 3 วันที่ผ่านมา

      yep i had to rewatch how to calculate the diameter of a binary tree, completely had forgotten about it.

    • @Everafterbreak_
      @Everafterbreak_ 3 วันที่ผ่านมา

      I had to do, Tree Diameter, bfs + dfs solution and deep understand it

  • @KrystianRatajczyk-ko5rb
    @KrystianRatajczyk-ko5rb 4 วันที่ผ่านมา +2

    I am glad and sooo proud of myself that i came up with the same solution like you and i was able to code it up!! Practice really pays off

    • @JamesBond-mq7pd
      @JamesBond-mq7pd 4 วันที่ผ่านมา

      how long have you been solving problems on leetcode?

    • @KrystianRatajczyk-ko5rb
      @KrystianRatajczyk-ko5rb 4 วันที่ผ่านมา

      @@JamesBond-mq7pdbit more than a year, in that time i solved 650 problems and you?

    • @JamesBond-mq7pd
      @JamesBond-mq7pd 4 วันที่ผ่านมา +1

      @@KrystianRatajczyk-ko5rb more than a year and solved 370 problems. Still can't solve hard problems

  • @nayankhanna2367
    @nayankhanna2367 3 วันที่ผ่านมา

    Great explanation, as always. Thank you!

  • @topsy_kreds
    @topsy_kreds 4 วันที่ผ่านมา

    thank you for all what you are doing, you are the best leetcode teacher on youtube.

  • @nox9765
    @nox9765 3 วันที่ผ่านมา

    Very good explanation, thank you neetcode!!!

  • @johnniewalkerjohnniewalker2459
    @johnniewalkerjohnniewalker2459 3 วันที่ผ่านมา

    Merry Christmas!!

  • @Chris-hz8lj
    @Chris-hz8lj 4 วันที่ผ่านมา

    Can you redo the reconstruct itinerary problem and go over the Hierholzer's algorithm for eulerian paths

  • @business_central
    @business_central 4 วันที่ผ่านมา +1

    why did we define n and m if we never used them though ?

    • @NeetCodeIO
      @NeetCodeIO  4 วันที่ผ่านมา +1

      Good question honestly, I guess since we used a defaultdict it wasn't necessary to get n and m

    • @business_central
      @business_central 4 วันที่ผ่านมา

      @@NeetCodeIO Thank you for the clarification ! I felt you only used it to clarify and share more about the thinking process but it was not necessary for the answer.
      Thanks again for everything you do. Really life saver!

  • @akshatpandey007
    @akshatpandey007 3 วันที่ผ่านมา

    oh i got the first and second max height of all the nodes and stored them in list, i knew that diameter can be in either three or both tree but i was not not able to guess which node should i take to connect bothe trees… so i again do a dfs to reroot tree on all nodes and got the minimum height of tree of all possible trees and joined them. The solution was linear time so it passed, but it gave me a headache to implement

    • @akshatpandey007
      @akshatpandey007 3 วันที่ผ่านมา

      but your solution was amazing and easy

  • @leetprep
    @leetprep 4 วันที่ผ่านมา

    This Christmas eve daily challenge is diabolical

  • @NBetweenStations
    @NBetweenStations 9 ชั่วโมงที่ผ่านมา

    Python heap is a min heap though. How could it be storing max values?

  • @prajapati-suraj
    @prajapati-suraj 3 วันที่ผ่านมา

    i was able to think of this approach but could not code it

  • @yankeehater237
    @yankeehater237 4 วันที่ผ่านมา +1

    Help me understand why we divide by 2? That’s the only part not making sense imo

    • @pratham.mp4
      @pratham.mp4 4 วันที่ผ่านมา +1

      We divide it by 2 to get the minimum path.
      For example: Let's say we have the maximum diameter from Tree1 as (1->2->3->4->5) and from Tree2 as (6->7->8->9->10). If we join Node (5) with Node (10) the diameter for the merged Tree will eventually increase (1->2->3->4->5->6->7->8->9->10), but we want to decrease it and get the minimum diameter of the merged tree. But if we divide the max diameter from both the trees by 2 and then try to merge it, we will get the minimum path. Take Node (3) from Tree1 and Node (8) from Tree2, the maximum diameter path for the merged Tree then will become (1->2->3->8->9->10) which will be the minimum diameter possible. That's why divide the max diameters by 2 and then +1 for adding the connection edge.

  • @codygao8323
    @codygao8323 3 วันที่ผ่านมา +1

    solved it within 20min, all it takes is the observation, the actual solution is straightforward to be honest.

  • @rjha5516
    @rjha5516 4 วันที่ผ่านมา

    I don't know python how to find solutions in java

  • @-ArnobBiswas
    @-ArnobBiswas 4 วันที่ผ่านมา +1

    Can someone explain to me why the final answer is like the max(d1,d2,1+ceil(d1/2)+ceil(d2/2))? I understand the 3rd term where we are trying to minimize the diameter after merging the two trees as asked in the problem and also explained in the video, but why exactly are we also considering d1 and d2 individually? I did not understand it.

    • @akashhjajoo
      @akashhjajoo 4 วันที่ผ่านมา +2

      Lets say first graph has d1 = 10, secnd one has just 2. 3rd arg will give 7, but the diameter will still be 10.

  • @karthi7102
    @karthi7102 4 วันที่ผ่านมา

    You are talking to the real goat.

  • @cooldudeachyut
    @cooldudeachyut 4 วันที่ผ่านมา

    I found the minimum diameter after merging the tree (without the simple formula, just manually going through all nodes and finding the minimum longest path from any node in a tree and adding them for both trees) but forgot to also consider that a larger tree can still contain the diameter by itself.

  • @floriankubiak7313
    @floriankubiak7313 3 วันที่ผ่านมา

    Meh, I came here because I didn't want to code up my solution, since it seems tiresome, so I hoped to find a simpler one. But you got the exact same one lol

    • @floriankubiak7313
      @floriankubiak7313 3 วันที่ผ่านมา

      Okay, a little different. I wasn't going to use a heap, but a 2-tuple with the two max values. Close enough.

  • @adityamwagh
    @adityamwagh 4 วันที่ผ่านมา +4

    wait, I'm first?

  • @csec0565
    @csec0565 4 วันที่ผ่านมา +1

    Why the hell this is not on top today.......
    Watched many other and just wasted time.

  • @cinemaantepichi5480
    @cinemaantepichi5480 4 วันที่ผ่านมา

    but how can we guarantee that 0 is always the root of the tree, leetcode did not mention in the problem statement, if 0 is not the root, how can we compare the children for max diameter?

    • @vivianpais3063
      @vivianpais3063 4 วันที่ผ่านมา

      It also updates the max_d for all children calls right? So that accounts for paths not going through 0. Suppose there is a diameter not going through 0: Some node on that diameter will be the child for which the function is called; then for the max paths for that child, the two branches that form the diameter will remain in the heap of that function call, thus creating the max_d for that function call which is then propagated upwards in the return values.

    • @floriankubiak7313
      @floriankubiak7313 3 วันที่ผ่านมา

      There is no root, but 0 is always the lowest index, hence if it's not empty you can choose it. If they aren't explicitly ordered, you just take any entry from the edges array.

  • @unlucky-777
    @unlucky-777 4 วันที่ผ่านมา

    It feels good when you can solve hard question in 15 minute without extra help