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

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ส.ค. 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/gah...
    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 TH-cam 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.ne...
    My Favorite Courses:
    Data Structures & Algorithms:
    - UCalifornia San Diego DSA: imp.i384100.ne...
    - Stanford Algorithms: imp.i384100.ne...
    - Python Data Structures: imp.i384100.ne...
    - Meta Coding Interview Prep: imp.i384100.ne...
    Python:
    - UMichigan Python for Everybody: imp.i384100.ne...
    - Python Mastery from MLNOW.ai: mlnow.ai/cours...
    - Google IT Automation w/ Python: imp.i384100.ne...
    Web Dev / Full Stack:
    - Meta Front-End Developer: imp.i384100.ne...
    - IBM Full Stack Developer: imp.i384100.ne...
    - Meta Back-End Developer: imp.i384100.ne...
    - John Hopkins HTML, CSS & JS: imp.i384100.ne...
    - IBM DevOps: imp.i384100.ne...
    Cloud Development:
    - AWS Fundamentals: imp.i384100.ne...
    - GCP Cloud Engineer: imp.i384100.ne...
    - Microsoft Azure Fundamentals: imp.i384100.ne...
    Game Development:
    - Michigan State Unity Development: imp.i384100.ne...
    - UColorado C++ for Unreal Engine: www.coursera.o...
    SQL & Data Science:
    - SQL by MLNOW.ai: mlnow.ai/cours...
    - Python for Data Science by MLNOW.ai: mlnow.ai/cours...
    - Google Data Analytics: imp.i384100.ne...
    - IBM Data Science: imp.i384100.ne...
    - IBM Data Engineer: imp.i384100.ne...
    Machine Learning & AI:
    - ML Mastery at MLNOW.ai: mlnow.ai/cours...
    - ML w/ Andrew Ng: www.coursera.o...
    - Deep Learning w/ Andrew Ng: imp.i384100.ne...

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

  • @GregHogg
    @GregHogg  หลายเดือนก่อน

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

  • @gaurangdeka
    @gaurangdeka หลายเดือนก่อน +16

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

  • @cbbforever
    @cbbforever หลายเดือนก่อน +3

    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  หลายเดือนก่อน

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

  • @helloworldcsofficial
    @helloworldcsofficial 9 วันที่ผ่านมา

    I think what trips us the most is ensuring we know the definition of height, depth, max height, max depth of a tree and/or node. In some sites, these are defined by number of edges but in others by number of nodes. Once you get these definitions right, the solution becomes even more clear. Thanks for the vid!

    • @GregHogg
      @GregHogg  9 วันที่ผ่านมา

      Yeah for sure, totally agree. No problem!

  • @servantofthelord8147
    @servantofthelord8147 หลายเดือนก่อน +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

  • @ravindraramachandra2237
    @ravindraramachandra2237 29 วันที่ผ่านมา

    Thank you... crisp and clear explanation!

  • @zxchh4629
    @zxchh4629 หลายเดือนก่อน

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

    • @GregHogg
      @GregHogg  หลายเดือนก่อน

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

  • @AtharvNaidu-mh2pm
    @AtharvNaidu-mh2pm วันที่ผ่านมา

    How are you such a good teacher bro, thanks so much this explanation is awesome

  • @nooraldeen6637
    @nooraldeen6637 4 หลายเดือนก่อน

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

    • @GregHogg
      @GregHogg  4 หลายเดือนก่อน +1

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

  • @andrewbrowne8498
    @andrewbrowne8498 หลายเดือนก่อน +2

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

  • @deed.9516
    @deed.9516 4 หลายเดือนก่อน

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

    • @GregHogg
      @GregHogg  4 หลายเดือนก่อน

      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:)

  • @user-pi9pn8ni6i
    @user-pi9pn8ni6i 9 วันที่ผ่านมา

    This must be medium.
    Thanks alot for the great content.

  • @moade5700
    @moade5700 12 วันที่ผ่านมา

    could you please expand on why a list is accessible from the scope of the method ?

  • @onurucar1112
    @onurucar1112 4 หลายเดือนก่อน

    Amazing and simple as usual!

    • @GregHogg
      @GregHogg  4 หลายเดือนก่อน

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

  • @user-no4hp5cd7o
    @user-no4hp5cd7o 3 หลายเดือนก่อน

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

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน +1

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

  • @MamoodXx
    @MamoodXx 3 หลายเดือนก่อน +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 TH-cam to find the solution or am I ruining my progress. Thank you

    • @GregHogg
      @GregHogg  3 หลายเดือนก่อน

      Looking up the solution is totally okay :)

    • @chi94
      @chi94 2 หลายเดือนก่อน +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-pi9pn8ni6i
      @user-pi9pn8ni6i 9 วันที่ผ่านมา

      ​@@chi94 by thinking logically 😅
      I am not saying it's not ok to look for solution on TH-cam or elsewhere but we should give it some tries to solve problems to strengthen our logical thinking.

  • @nooraldeen6637
    @nooraldeen6637 4 หลายเดือนก่อน

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

    • @GregHogg
      @GregHogg  4 หลายเดือนก่อน

      You could, yes :)

  • @siddhantkumar9492
    @siddhantkumar9492 3 หลายเดือนก่อน

    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  3 หลายเดือนก่อน

      Which parameter would that be?

    • @siddhantkumar9492
      @siddhantkumar9492 3 หลายเดือนก่อน

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

  • @aakashs1806
    @aakashs1806 28 วันที่ผ่านมา

    Looks like height computation

  • @itvaya
    @itvaya 4 หลายเดือนก่อน

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

    • @GregHogg
      @GregHogg  4 หลายเดือนก่อน

      I use miro :)