Diameter of Binary Tree - Leetcode 543 - Python

Поделиться
HTML-код
  • Опубликовано: 23 янв 2025

Комментарии • 33

  • @NeetCodeIO
    @NeetCodeIO  5 месяцев назад +27

    I've already made a solution for this problem but i thought it could be improved so i rerecorded it.
    If you notice any other problems in the NeetCode 150 (neetcode.io/practice) where the video solution could be improved, please let me know!

    • @floatingpoint7629
      @floatingpoint7629 5 месяцев назад +1

      thanks for this, the explanation is much better then the one before
      👍

    • @jeysiva_old
      @jeysiva_old 5 месяцев назад

      This explanation is better and clear, Thanks

    • @chronobytes971
      @chronobytes971 4 месяца назад +1

      Much better than the previous one! I have understood it at once. Thank you very much

    • @jrose2082
      @jrose2082 4 месяца назад

      you’re awesome dude

    • @dzccccc
      @dzccccc 3 месяца назад

      Thank you for everything you do :)

  • @galkk3
    @galkk3 5 месяцев назад +9

    another option that I prefer to use regarding the res variable is to declare it as an array like res = [0]
    and then any time I want to use it it is like this: res[0] = ...

  • @NikithM-vj6pn
    @NikithM-vj6pn 5 месяцев назад +10

    I know it is a big thing to ask , but can you make short videos for all the 150 neetcode problems , suggesting the most important parts / techniques / approaches required to solve the problem?
    Huge thanks for your short videos they are helping for quick revision :)

  • @NikithM-vj6pn
    @NikithM-vj6pn 5 месяцев назад +3

    could you do reverse linked list nodes in pairs of k?

  • @benmyths
    @benmyths 5 месяцев назад +1

    brother just now i started solving this problem and i see your latest video on it. W timing haha!

  • @mercymutuku4525
    @mercymutuku4525 Месяц назад

    Similar to your solution for Binary Tree Maximum Path Sum, except now we don't care about the node values ❤‍🔥🙏

  • @xaq_01_02
    @xaq_01_02 5 месяцев назад

    Great timing this explanation is super clean!

  • @davitmodebadze9707
    @davitmodebadze9707 5 месяцев назад +19

    For a second I thought leetcode posted the daily problem earlier than usual

  • @LuisSolorzano-p9u
    @LuisSolorzano-p9u 4 месяца назад

    Hey thanks for the video! When I was doing this problem I knew that I could use a global variable, but in bigger coding applications global variables should be avoided.
    Do you believe using global variables for interview sessions is okay? Is it too much to ask an interviewer if I'm allowed to use them rather than assume I can use them? Thanks!

    • @dzccccc
      @dzccccc 3 месяца назад +1

      A bit late but I saw either a video or podcast with Neetcode where he mentioned he passed the variable as an argument instead of using global variable, and his interviewer was asking him how he could simplify it, and he said he believed that they wanted him to use a global variable to simplify it

  • @DerekGomez-n1w
    @DerekGomez-n1w Месяц назад +1

    if you're confused by the return statement remember:
    height = 1 + max(left, right)
    diameter = left + right

  • @rryt000
    @rryt000 4 месяца назад +3

    can someone explain why the space complexity would be O(h)?

    • @hi-ew2ox
      @hi-ew2ox 4 месяца назад +2

      in this case since we're using recursion, the space complexity is based on the maximum number of nodes we're storing in the callstack. this would be equal to the height of the tree, since that is when you have reached the deepest part of the tree from the root.
      in the worst case, if the tree is completely vertical and all the nodes are just stacked on top of each other, the space complexity would be O(N) where N is the number of nodes.
      in the best case, it would be O(h), where h is the height, e.g. as mentioned in the video if the tree was balanced it would be O(logN) since theres a logarithmic relationship between the height and the number of notes (at level h in the balanced tree, there are 2^h nodes)

    • @dusvn1484
      @dusvn1484 3 месяца назад

      I have same problem to understand this so I will try to help you on the shortest way.
      The space complexity of recursion dependent on maximum recursive calls at the moment.So as you can see on neet example maximum deep where we can be is the height of tree which value is 3 in video example. Log with base 2 of N where N is equal to 8 is 3.
      So we see the relation of log base 2 of 8 = height of the tree.
      Write me some feedback if this help you :D

  • @bennycai7552
    @bennycai7552 2 месяца назад +1

    I found one thing. When you do 1 + max(left, right), what if curr doesn't have left and right children? The height should be 0, but 1 + max(left, right) will make it 1.

    • @DerekGomez-n1w
      @DerekGomez-n1w Месяц назад +2

      if curr is non-null, the height is 1.

  • @okbro4368
    @okbro4368 7 дней назад +1

    I hate recursion, not hard to understand recursion but it is super hard to come up with a solution that using recursion for beginner

  • @mohanedomer9081
    @mohanedomer9081 5 месяцев назад

    if you have the time plz do 493. Reverse Pairs . I couldn't understand it at all plz

  • @Emorinken
    @Emorinken 4 месяца назад +1

    I was wondering why this had just a little over 200 likes, then i realized it was just posted a month ago

  • @PDSREACTION
    @PDSREACTION 4 месяца назад +4

    if this is easy then Im mike jackson

  • @maanas_sehgal
    @maanas_sehgal 5 месяцев назад +5

    Ayy neetcode video at 2:30 AM India❤

  • @mohamedabaza9700
    @mohamedabaza9700 4 месяца назад +1

    legend