Master Data Structures & Algorithms For FREE at AlgoMap.io!
@gaurangdeka4 ай бұрын
Stumbled upon this after having a hard time understanding Neetcode's explanation. This is so much better!
@KAVINSAGARR4 ай бұрын
same here !!
@bigk9000Ай бұрын
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-n1w22 сағат бұрын
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
@swapnilrao98818 күн бұрын
such a good explanation, i really couldnt wrap my head around the recursion until i watched this video. leetcode truly humbles you!
@andrewbrowne84984 ай бұрын
You can also just use "nonlocal largest_diameter" at the beginning of the function
@cbbforever4 ай бұрын
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.
@GregHogg4 ай бұрын
Awe thank you so much! And yes this should be a medium haha
@helloworldcsofficial3 ай бұрын
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!
@GregHogg3 ай бұрын
Yeah for sure, totally agree. No problem!
@servantofthelord81474 ай бұрын
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
@deed.95167 ай бұрын
Thank you for this detailed explanation! I ran into the same error with the nested function, so I appreciate you covering that!
@GregHogg7 ай бұрын
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:)
@ravindraramachandra22374 ай бұрын
Thank you... crisp and clear explanation!
@AtharvNaidu-mh2pm3 ай бұрын
How are you such a good teacher bro, thanks so much this explanation is awesome
@nooraldeen66377 ай бұрын
Also I believe you can just do nonlocal largest_diameter inside of the height function instead of making it into a one-item list
@GregHogg7 ай бұрын
You could, yes :)
@zxchh46294 ай бұрын
thanks a lot, by far the best solution i've seen for this one on here
@GregHogg4 ай бұрын
Glad to hear it, this can be a tricky one!
@nooraldeen66377 ай бұрын
Oh wow your explaintations are extremely detailed and clear. Keep it up! Thank you
@GregHogg7 ай бұрын
For this question in particular, I'm actually super glad to hear it. Thanks so much :)
@__chroma__86602 ай бұрын
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?
@moade57003 ай бұрын
could you please expand on why a list is accessible from the scope of the method ?
@MamoodXx6 ай бұрын
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
@GregHogg6 ай бұрын
Looking up the solution is totally okay :)
@chi946 ай бұрын
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-p7e9d3 ай бұрын
@@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.
@MohammedAli-p7e9d3 ай бұрын
This must be medium. Thanks alot for the great content.
@siddhantkumar94927 ай бұрын
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!
@GregHogg7 ай бұрын
Which parameter would that be?
@siddhantkumar94927 ай бұрын
@@GregHogg I was wrong, my bad. The above logic is only applicable when that parameter doesn't change within the nested function
@aakashs18064 ай бұрын
Looks like height computation
@onurucar11127 ай бұрын
Amazing and simple as usual!
@GregHogg7 ай бұрын
Thanks so much for the support, and very glad to hear it!!
@aradhyadhruv-y4l6 ай бұрын
Your explanations are really good. Please make a video on LRU cache as well
@GregHogg6 ай бұрын
Thank you! And alright I'll do that one could be a little bit though
@itvaya7 ай бұрын
can you please tell me which app you are using for drawing while explaining