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

preview_player
Показать описание


Please check my playlists for free DSA problem solutions:

My Favorite Courses:

Data Structures & Algorithms:

Python:

Web Dev / Full Stack:

Cloud Development:

Game Development:

SQL & Data Science:

Machine Learning & AI:
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms For FREE at AlgoMap.io!

GregHogg
Автор

Stumbled upon this after having a hard time understanding Neetcode's explanation. This is so much better!

gaurangdeka
Автор

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.

cbbforever
Автор

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)

bigk
Автор

Helped me understand this problem better than Neetcode's solution. Thanks.

nasty_jack
Автор

You can also just use "nonlocal largest_diameter" at the beginning of the function

andrewbrowne
Автор

such a good explanation, i really couldnt wrap my head around the recursion until i watched this video. leetcode truly humbles you!

swapnilrao
Автор

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!

helloworldcsofficial
Автор

Your explanation is very clear and helpful, thanks!
Regarding your implementation, small suggestions:
1. Never write something like "if root is None", simply use "if not root".
2. To handle the scope issue of the "largest_diameter" variable, its better in my opinion to use the "nonlocal largest_diameter" syntax, or to simply pass it as a parameter to the DFS method. You could also add a simple __init__ method to your class and state it there. The hack-y approach of putting it in a list might confuse an interviewer.

YotamNHL
Автор

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

hyperboliq
Автор

thanks a lot, by far the best solution i've seen for this one on here

zxchh
Автор

To avoid the nonlocal variable issue, just use self! I feel like that's the easiest way to define a global var in Python.

shxdow
Автор

Thank you for this detailed explanation! I ran into the same error with the nested function, so I appreciate you covering that!

deed.
Автор

Oh wow your explaintations are extremely detailed and clear. Keep it up! Thank you

nooraldeen
Автор

How are you such a good teacher bro, thanks so much this explanation is awesome

coolcat
Автор

Thank you... crisp and clear explanation!

ravindraramachandra
Автор

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 YouTube to find the solution or am I ruining my progress. Thank you

MamoodXx
Автор

you could add self. to the largest_diameter like self.largest_diameter to solve the local variable access issue

akshaypendyala
Автор

can you please tell me which app you are using for drawing while explaining

itvaya
Автор

your explanation is good but the part of making it a list isnt. Instead of making it a list, just add self.<var> to it, and the explanation is that its a variable from the previous function where your dfs function is nested, so in order to access it, you need to you self to instantiate it, just like you normally do when you use the __init__:constructor.

laodrofotic
visit shbcf.ru