Тёмный

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

Greg Hogg
Подписаться 201 тыс.
Просмотров 6 тыс.
50% 1

Опубликовано:

 

20 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 39   
@GregHogg
@GregHogg 2 месяца назад
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@gaurangdeka
@gaurangdeka 3 месяца назад
Stumbled upon this after having a hard time understanding Neetcode's explanation. This is so much better!
@KAVINSAGARR
@KAVINSAGARR 3 месяца назад
same here !!
@cbbforever
@cbbforever 3 месяца назад
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 3 месяца назад
Awe thank you so much! And yes this should be a medium haha
@helloworldcsofficial
@helloworldcsofficial 2 месяца назад
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 2 месяца назад
Yeah for sure, totally agree. No problem!
@andrewbrowne8498
@andrewbrowne8498 3 месяца назад
You can also just use "nonlocal largest_diameter" at the beginning of the function
@bigk9000
@bigk9000 26 дней назад
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)
@servantofthelord8147
@servantofthelord8147 3 месяца назад
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 2 месяца назад
Thank you... crisp and clear explanation!
@zxchh4629
@zxchh4629 3 месяца назад
thanks a lot, by far the best solution i've seen for this one on here
@GregHogg
@GregHogg 3 месяца назад
Glad to hear it, this can be a tricky one!
@deed.9516
@deed.9516 6 месяцев назад
Thank you for this detailed explanation! I ran into the same error with the nested function, so I appreciate you covering that!
@GregHogg
@GregHogg 6 месяцев назад
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:)
@nooraldeen6637
@nooraldeen6637 6 месяцев назад
Oh wow your explaintations are extremely detailed and clear. Keep it up! Thank you
@GregHogg
@GregHogg 6 месяцев назад
For this question in particular, I'm actually super glad to hear it. Thanks so much :)
@AtharvNaidu-mh2pm
@AtharvNaidu-mh2pm 2 месяца назад
How are you such a good teacher bro, thanks so much this explanation is awesome
@onurucar1112
@onurucar1112 6 месяцев назад
Amazing and simple as usual!
@GregHogg
@GregHogg 6 месяцев назад
Thanks so much for the support, and very glad to hear it!!
@aakashs1806
@aakashs1806 2 месяца назад
Looks like height computation
@nooraldeen6637
@nooraldeen6637 6 месяцев назад
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 6 месяцев назад
You could, yes :)
@MohammedAli-p7e9d
@MohammedAli-p7e9d 2 месяца назад
This must be medium. Thanks alot for the great content.
@__chroma__8660
@__chroma__8660 Месяц назад
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 2 месяца назад
could you please expand on why a list is accessible from the scope of the method ?
@aradhyadhruv-y4l
@aradhyadhruv-y4l 5 месяцев назад
Your explanations are really good. Please make a video on LRU cache as well
@GregHogg
@GregHogg 5 месяцев назад
Thank you! And alright I'll do that one could be a little bit though
@MamoodXx
@MamoodXx 5 месяцев назад
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 RU-vid to find the solution or am I ruining my progress. Thank you
@GregHogg
@GregHogg 4 месяца назад
Looking up the solution is totally okay :)
@chi94
@chi94 4 месяца назад
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 2 месяца назад
​@@chi94 by thinking logically 😅 I am not saying it's not ok to look for solution on RU-vid or elsewhere but we should give it some tries to solve problems to strengthen our logical thinking.
@itvaya
@itvaya 5 месяцев назад
can you please tell me which app you are using for drawing while explaining
@GregHogg
@GregHogg 5 месяцев назад
I use miro :)
@siddhantkumar9492
@siddhantkumar9492 5 месяцев назад
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 5 месяцев назад
Which parameter would that be?
@siddhantkumar9492
@siddhantkumar9492 5 месяцев назад
@@GregHogg I was wrong, my bad. The above logic is only applicable when that parameter doesn't change within the nested function
@JSH1994
@JSH1994 21 день назад
please solve more problems :)
Далее
Same Binary Tree - Leetcode 100 - Trees (Python)
5:55
Просмотров 2,8 тыс.
aespa 에스파 'Whiplash' MV
03:11
Просмотров 7 млн
Павел Дедищев - «Как кайф»
42:08
Просмотров 469 тыс.
Top 7 Data Structures for Interviews Explained SIMPLY
13:02
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
Rotting Oranges - Leetcode 994 - Graphs (Python)
16:09
Просмотров 2,8 тыс.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Просмотров 267 тыс.
The Sad Reality of Being a Data Scientist
8:55
Просмотров 79 тыс.
aespa 에스파 'Whiplash' MV
03:11
Просмотров 7 млн