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

  Рет қаралды 7,658

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 41
@GregHogg
@GregHogg 4 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@gaurangdeka
@gaurangdeka 4 ай бұрын
Stumbled upon this after having a hard time understanding Neetcode's explanation. This is so much better!
@KAVINSAGARR
@KAVINSAGARR 4 ай бұрын
same here !!
@bigk9000
@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-n1w
@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
@swapnilrao9881
@swapnilrao9881 8 күн бұрын
such a good explanation, i really couldnt wrap my head around the recursion until i watched this video. leetcode truly humbles you!
@andrewbrowne8498
@andrewbrowne8498 4 ай бұрын
You can also just use "nonlocal largest_diameter" at the beginning of the function
@helloworldcsofficial
@helloworldcsofficial 3 ай бұрын
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 3 ай бұрын
Yeah for sure, totally agree. No problem!
@cbbforever
@cbbforever 4 ай бұрын
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 4 ай бұрын
Awe thank you so much! And yes this should be a medium haha
@servantofthelord8147
@servantofthelord8147 4 ай бұрын
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 4 ай бұрын
Thank you... crisp and clear explanation!
@deed.9516
@deed.9516 7 ай бұрын
Thank you for this detailed explanation! I ran into the same error with the nested function, so I appreciate you covering that!
@GregHogg
@GregHogg 7 ай бұрын
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:)
@zxchh4629
@zxchh4629 4 ай бұрын
thanks a lot, by far the best solution i've seen for this one on here
@GregHogg
@GregHogg 4 ай бұрын
Glad to hear it, this can be a tricky one!
@nooraldeen6637
@nooraldeen6637 7 ай бұрын
Oh wow your explaintations are extremely detailed and clear. Keep it up! Thank you
@GregHogg
@GregHogg 7 ай бұрын
For this question in particular, I'm actually super glad to hear it. Thanks so much :)
@AtharvNaidu-mh2pm
@AtharvNaidu-mh2pm 3 ай бұрын
How are you such a good teacher bro, thanks so much this explanation is awesome
@onurucar1112
@onurucar1112 7 ай бұрын
Amazing and simple as usual!
@GregHogg
@GregHogg 7 ай бұрын
Thanks so much for the support, and very glad to hear it!!
@moade5700
@moade5700 3 ай бұрын
could you please expand on why a list is accessible from the scope of the method ?
@nooraldeen6637
@nooraldeen6637 7 ай бұрын
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 7 ай бұрын
You could, yes :)
@itvaya
@itvaya 7 ай бұрын
can you please tell me which app you are using for drawing while explaining
@GregHogg
@GregHogg 7 ай бұрын
I use miro :)
@MohammedAli-p7e9d
@MohammedAli-p7e9d 3 ай бұрын
This must be medium. Thanks alot for the great content.
@__chroma__8660
@__chroma__8660 2 ай бұрын
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?
@aradhyadhruv-y4l
@aradhyadhruv-y4l 6 ай бұрын
Your explanations are really good. Please make a video on LRU cache as well
@GregHogg
@GregHogg 6 ай бұрын
Thank you! And alright I'll do that one could be a little bit though
@MamoodXx
@MamoodXx 6 ай бұрын
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
@GregHogg
@GregHogg 6 ай бұрын
Looking up the solution is totally okay :)
@chi94
@chi94 6 ай бұрын
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-p7e9d
@MohammedAli-p7e9d 3 ай бұрын
​@@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.
@aakashs1806
@aakashs1806 4 ай бұрын
Looks like height computation
@siddhantkumar9492
@siddhantkumar9492 7 ай бұрын
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 7 ай бұрын
Which parameter would that be?
@siddhantkumar9492
@siddhantkumar9492 7 ай бұрын
@@GregHogg I was wrong, my bad. The above logic is only applicable when that parameter doesn't change within the nested function
@JSH1994
@JSH1994 Ай бұрын
please solve more problems :)
Same Binary Tree - Leetcode 100 - Trees (Python)
5:55
Greg Hogg
Рет қаралды 3,5 М.
Diameter of Binary Tree - Leetcode 543 - Python
7:26
NeetCodeIO
Рет қаралды 32 М.
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 111 МЛН
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 15 МЛН
Pacific Atlantic Water Flow - Leetcode 417 - Graphs (Python)
17:10
L16. Diameter of Binary Tree | C++ | Java
13:47
take U forward
Рет қаралды 378 М.
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 237 М.
Rotting Oranges - Leetcode 994 - Graphs (Python)
16:09
Greg Hogg
Рет қаралды 3,4 М.
Diameter of Binary Tree | Simple Story | Amazon | Leetcode 543
19:38
codestorywithMIK
Рет қаралды 7 М.
Diameter of a Binary Tree (Code/ Algorithm)
17:15
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 94 М.
Don't Learn Machine Learning, Instead learn this!
6:21
Deepchand O A
Рет қаралды 97 М.
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Greg Hogg
Рет қаралды 10 М.
1, 2, 3, 4, 5, 6, 7, 8, 9 🙈⚽️
00:46
Celine Dept
Рет қаралды 111 МЛН