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

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

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 22 сағат бұрын
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
@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
@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!
@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
@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:)
@ravindraramachandra2237
@ravindraramachandra2237 4 ай бұрын
Thank you... crisp and clear explanation!
@AtharvNaidu-mh2pm
@AtharvNaidu-mh2pm 3 ай бұрын
How are you such a good teacher bro, thanks so much this explanation is awesome
@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 :)
@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 :)
@__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?
@moade5700
@moade5700 3 ай бұрын
could you please expand on why a list is accessible from the scope of the method ?
@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.
@MohammedAli-p7e9d
@MohammedAli-p7e9d 3 ай бұрын
This must be medium. Thanks alot for the great content.
@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
@aakashs1806
@aakashs1806 4 ай бұрын
Looks like height computation
@onurucar1112
@onurucar1112 7 ай бұрын
Amazing and simple as usual!
@GregHogg
@GregHogg 7 ай бұрын
Thanks so much for the support, and very glad to hear it!!
@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
@itvaya
@itvaya 7 ай бұрын
can you please tell me which app you are using for drawing while explaining
@GregHogg
@GregHogg 7 ай бұрын
I use miro :)
@JSH1994
@JSH1994 Ай бұрын
please solve more problems :)
Same Binary Tree - Leetcode 100 - Trees (Python)
5:55
Greg Hogg
Рет қаралды 3,5 М.
Rotting Oranges - Leetcode 994 - Graphs (Python)
16:09
Greg Hogg
Рет қаралды 3,4 М.
24 Часа в БОУЛИНГЕ !
27:03
A4
Рет қаралды 7 МЛН
UFC 287 : Перейра VS Адесанья 2
6:02
Setanta Sports UFC
Рет қаралды 486 М.
JISOO - ‘꽃(FLOWER)’ M/V
3:05
BLACKPINK
Рет қаралды 137 МЛН
Война Семей - ВСЕ СЕРИИ, 1 сезон (серии 1-20)
7:40:31
Семейные Сериалы
Рет қаралды 1,6 МЛН
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 237 М.
Pacific Atlantic Water Flow - Leetcode 417 - Graphs (Python)
17:10
Diameter Of Binary Tree ( Basic Approach) - JAVA
19:46
Coding Ninjas
Рет қаралды 26 М.
Clone Graph - Leetcode 133 - Graphs (Python)
13:38
Greg Hogg
Рет қаралды 3,8 М.
DIAMETER OF A BINARY TREE | LEETCODE 543 | PYTHON DFS SOLUTION
10:02
Word Search - Leetcode 79 - Recursive Backtracking (Python)
12:24
24 Часа в БОУЛИНГЕ !
27:03
A4
Рет қаралды 7 МЛН