Master Data Structures & Algorithms For FREE at AlgoMap.io!
@gaurangdeka6 ай бұрын
Stumbled upon this after having a hard time understanding Neetcode's explanation. This is so much better!
@KAVINSAGARR5 ай бұрын
same here !!
@cbbforever5 ай бұрын
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.
@GregHogg5 ай бұрын
Awe thank you so much! And yes this should be a medium haha
@swapnilrao9881Ай бұрын
such a good explanation, i really couldnt wrap my head around the recursion until i watched this video. leetcode truly humbles you!
@bigk90003 ай бұрын
In regards to the variable scoping topic: Instead of creating a single field array, you can also use the `nonlocal` keyword as well. Though, it does add an extra line to your code, as it'll look something like this: nonlocal largest_diameter largest_diameter = max(largest_diameter, left_height + right_height)
@DerekGomez-n1wАй бұрын
I do this, too! Had to learn this the hardway haha I spent like an hour researching scoping in python and why the global keyword doesn't work
@abdulrahmanbadran67132 күн бұрын
@@DerekGomez-n1w why global won't work?
@helloworldcsofficial4 ай бұрын
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!
@GregHogg4 ай бұрын
Yeah for sure, totally agree. No problem!
@PratikshaNaik-d9i24 күн бұрын
Such crystal clear explanation.
@andrewbrowne84986 ай бұрын
You can also just use "nonlocal largest_diameter" at the beginning of the function
@yingxie997427 күн бұрын
Thanks, Greg ! Very clear ! 🙏👍
@zxchh46295 ай бұрын
thanks a lot, by far the best solution i've seen for this one on here
@GregHogg5 ай бұрын
Glad to hear it, this can be a tricky one!
@ravindraramachandra22375 ай бұрын
Thank you... crisp and clear explanation!
@nooraldeen66378 ай бұрын
Oh wow your explaintations are extremely detailed and clear. Keep it up! Thank you
@GregHogg8 ай бұрын
For this question in particular, I'm actually super glad to hear it. Thanks so much :)
@deed.95168 ай бұрын
Thank you for this detailed explanation! I ran into the same error with the nested function, so I appreciate you covering that!
@GregHogg8 ай бұрын
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:)
@AtharvNaidu-mh2pm4 ай бұрын
How are you such a good teacher bro, thanks so much this explanation is awesome
@servantofthelord81476 ай бұрын
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
@onurucar11128 ай бұрын
Amazing and simple as usual!
@GregHogg8 ай бұрын
Thanks so much for the support, and very glad to hear it!!
@MohammedAli-p7e9d4 ай бұрын
This must be medium. Thanks alot for the great content.
@aradhyadhruv-y4l7 ай бұрын
Your explanations are really good. Please make a video on LRU cache as well
@GregHogg7 ай бұрын
Thank you! And alright I'll do that one could be a little bit though
@nooraldeen66378 ай бұрын
Also I believe you can just do nonlocal largest_diameter inside of the height function instead of making it into a one-item list
@GregHogg8 ай бұрын
You could, yes :)
@MamoodXx7 ай бұрын
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 KZbin to find the solution or am I ruining my progress. Thank you
@GregHogg7 ай бұрын
Looking up the solution is totally okay :)
@chi947 ай бұрын
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!
@MohammedAli-p7e9d4 ай бұрын
@@chi94 by thinking logically 😅 I am not saying it's not ok to look for solution on KZbin or elsewhere but we should give it some tries to solve problems to strengthen our logical thinking.
@moade57004 ай бұрын
could you please expand on why a list is accessible from the scope of the method ?
@__chroma__86604 ай бұрын
I have a question. Instead of calculating the withing the height function, i calculated the diameter at the end by using diameter = height(root.left) + height(root.right). This worked for most test cases except one. Why do I have to calculate the diameter within the height function and not after?
@itvaya8 ай бұрын
can you please tell me which app you are using for drawing while explaining
@GregHogg8 ай бұрын
I use miro :)
@aakashs18065 ай бұрын
Looks like height computation
@siddhantkumar94928 ай бұрын
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!
@GregHogg8 ай бұрын
Which parameter would that be?
@siddhantkumar94928 ай бұрын
@@GregHogg I was wrong, my bad. The above logic is only applicable when that parameter doesn't change within the nested function