Diameter of Binary Tree - Leetcode 543 - Trees (Python)
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
Master Data Structures & Algorithms For FREE at AlgoMap.io!
Stumbled upon this after having a hard time understanding Neetcode's explanation. This is so much better!
same here !!
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.
Awe thank you so much! And yes this should be a medium haha
Thank you... crisp and clear explanation!
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
thanks a lot, by far the best solution i've seen for this one on here
Glad to hear it, this can be a tricky one!
Oh wow your explaintations are extremely detailed and clear. Keep it up! Thank you
For this question in particular, I'm actually super glad to hear it. Thanks so much :)
You can also just use "nonlocal largest_diameter" at the beginning of the function
Thank you for this detailed explanation! I ran into the same error with the nested function, so I appreciate you covering that!
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:)
Amazing and simple as usual!
Thanks so much for the support, and very glad to hear it!!
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
Looking up the solution is totally okay :)
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!
Your explanations are really good. Please make a video on LRU cache as well
Thank you! And alright I'll do that one could be a little bit though
Looks like height computation
can you please tell me which app you are using for drawing while explaining
I use miro :)
Also I believe you can just do
nonlocal largest_diameter inside of the height function instead of making it into a one-item list
You could, yes :)
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!
Which parameter would that be?
@@GregHogg I was wrong, my bad. The above logic is only applicable when that parameter doesn't change within the nested function