Diameter of Binary Tree - Leetcode 543 - Trees (Python)

Sdílet
Vložit
  • čas přidán 7. 04. 2024
  • Master Data Structures & Algorithms for FREE at AlgoMap.io/
    Code solutions in Python, Java, C++ and JS for this can be found at my GitHub repo here: github.com/gahogg/Leetcode-So...
    Complete DSA Pathway Zero to Hero: • Data Structures & Algo...
    Please check my playlists for free DSA problem solutions:
    • Fundamental DSA Theory
    • Array & String Questions
    • 2 Pointers Questions
    • Sliding Window Questions
    • Binary Search Questions
    • Stack Questions
    • Linked List Questions
    • Tree Questions
    • Heap Questions
    • Recursive Backtracking...
    • Graph Questions
    • Dynamic Programming (D...
    My Data Science & ML CZcams Playlist: • Greg's Path to Become ...
    Learn Python and Data Science FASTER at mlnow.ai :)
    Support the content: / @greghogg
    Follow me on Instagram: / greghogg5
    Connect with me on LinkedIn: / greghogg
    Follow me on TikTok: / greghogg5
    Coursera Plus: imp.i384100.net/P0E3J6
    My Favorite Courses:
    Data Structures & Algorithms:
    - UCalifornia San Diego DSA: imp.i384100.net/LP31oV
    - Stanford Algorithms: imp.i384100.net/vNBoxd
    - Python Data Structures: imp.i384100.net/NkZn47
    - Meta Coding Interview Prep: imp.i384100.net/Y96rBJ
    Python:
    - UMichigan Python for Everybody: imp.i384100.net/QOLM73
    - Python Mastery from MLNOW.ai: mlnow.ai/course-material/python/
    - Google IT Automation w/ Python: imp.i384100.net/5g6Xyj
    Web Dev / Full Stack:
    - Meta Front-End Developer: imp.i384100.net/q4Jemy
    - IBM Full Stack Developer: imp.i384100.net/Gj9dMn
    - Meta Back-End Developer: imp.i384100.net/xkW0r5
    - John Hopkins HTML, CSS & JS: imp.i384100.net/QyoRAA
    - IBM DevOps: imp.i384100.net/kjd2r0
    Cloud Development:
    - AWS Fundamentals: imp.i384100.net/anqBjZ
    - GCP Cloud Engineer: imp.i384100.net/g1jvqB
    - Microsoft Azure Fundamentals: imp.i384100.net/EKm5O4
    Game Development:
    - Michigan State Unity Development: imp.i384100.net/6eOBnr
    - UColorado C++ for Unreal Engine: www.coursera.org/specializati...
    SQL & Data Science:
    - SQL by MLNOW.ai: mlnow.ai/course-material/sql/
    - Python for Data Science by MLNOW.ai: mlnow.ai/course-material/data...
    - Google Data Analytics: imp.i384100.net/1rkWAR
    - IBM Data Science: imp.i384100.net/P0ZRL6
    - IBM Data Engineer: imp.i384100.net/4PbZyZ
    Machine Learning & AI:
    - ML Mastery at MLNOW.ai: mlnow.ai/course-material/ml/
    - ML w/ Andrew Ng: www.coursera.org/specializati...
    - Deep Learning w/ Andrew Ng: imp.i384100.net/a1kjJj

Komentáře • 29

  • @GregHogg
    @GregHogg  Před 3 dny

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @gaurangdeka
    @gaurangdeka Před měsícem +12

    Stumbled upon this after having a hard time understanding Neetcode's explanation. This is so much better!

  • @cbbforever
    @cbbforever Před 8 dny +2

    the best explanation in the whole universe, and don't know why those bad solution vedio got so many views, yours deserves more.
    PS: I think this problem should labelled as medium.

    • @GregHogg
      @GregHogg  Před 8 dny

      Awe thank you so much! And yes this should be a medium haha

  • @ravindraramachandra2237

    Thank you... crisp and clear explanation!

  • @servantofthelord8147
    @servantofthelord8147 Před 28 dny +5

    Instead of using that list trick at the end, couldn't we just define "self.largest_diameter=0" then just use self.largest_diameter everywhere that we reference it? This worked for me :
    class Solution:
    def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
    self.max_diam = 0

    def height(root):
    if not root:
    return 0
    left_height = height(root.left)
    right_height = height(root.right)
    diam = left_height + right_height
    self.max_diam = max(diam,self.max_diam)
    return max(left_height,right_height)+1
    height(root)
    return self.max_diam

  • @zxchh4629
    @zxchh4629 Před 6 dny

    thanks a lot, by far the best solution i've seen for this one on here

    • @GregHogg
      @GregHogg  Před 6 dny

      Glad to hear it, this can be a tricky one!

  • @nooraldeen6637
    @nooraldeen6637 Před 3 měsíci

    Oh wow your explaintations are extremely detailed and clear. Keep it up! Thank you

    • @GregHogg
      @GregHogg  Před 3 měsíci +1

      For this question in particular, I'm actually super glad to hear it. Thanks so much :)

  • @andrewbrowne8498
    @andrewbrowne8498 Před 24 dny +1

    You can also just use "nonlocal largest_diameter" at the beginning of the function

  • @deed.9516
    @deed.9516 Před 3 měsíci

    Thank you for this detailed explanation! I ran into the same error with the nested function, so I appreciate you covering that!

    • @GregHogg
      @GregHogg  Před 3 měsíci

      Yeah that's a super annoying and common pitfall, I was confused with it for a long time too... Hopefully I helped explain that decently:)

  • @onurucar1112
    @onurucar1112 Před 3 měsíci

    Amazing and simple as usual!

    • @GregHogg
      @GregHogg  Před 3 měsíci

      Thanks so much for the support, and very glad to hear it!!

  • @MamoodXx
    @MamoodXx Před 2 měsíci +2

    I just started algorithms after doing oop, I feel like Im always one step from solving the problem but I never find that step, is ok that Im going to CZcams to find the solution or am I ruining my progress. Thank you

    • @GregHogg
      @GregHogg  Před 2 měsíci

      Looking up the solution is totally okay :)

    • @chi94
      @chi94 Před 2 měsíci +1

      you're meant to look at the solution. How are you suppose to solve an algorithm question when you've never encountered it? Humans aren't aliens!

  • @user-no4hp5cd7o
    @user-no4hp5cd7o Před 2 měsíci

    Your explanations are really good. Please make a video on LRU cache as well

    • @GregHogg
      @GregHogg  Před 2 měsíci +1

      Thank you! And alright I'll do that one could be a little bit though

  • @aakashs1806
    @aakashs1806 Před 2 dny

    Looks like height computation

  • @itvaya
    @itvaya Před 3 měsíci

    can you please tell me which app you are using for drawing while explaining

  • @nooraldeen6637
    @nooraldeen6637 Před 3 měsíci

    Also I believe you can just do
    nonlocal largest_diameter inside of the height function instead of making it into a one-item list

  • @siddhantkumar9492
    @siddhantkumar9492 Před 3 měsíci

    I believe its not required to pass the same parameter to the nested function that is already a part of the parent function, we can directly access it. Feel free to correct me if I'm on the wrong direction!

    • @GregHogg
      @GregHogg  Před 3 měsíci

      Which parameter would that be?

    • @siddhantkumar9492
      @siddhantkumar9492 Před 3 měsíci

      @@GregHogg I was wrong, my bad. The above logic is only applicable when that parameter doesn't change within the nested function