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...
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
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!
Yeah for sure, totally agree. No problem!
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
Thank you... crisp and clear explanation!
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!
How are you such a good teacher bro, thanks so much this explanation is awesome
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:)
This must be medium.
Thanks alot for the great content.
could you please expand on why a list is accessible from the scope of the method ?
Amazing and simple as usual!
Thanks so much for the support, and very glad to hear it!!
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
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
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!
@@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.
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
Looks like height computation
can you please tell me which app you are using for drawing while explaining
I use miro :)