Diameter of Binary Tree - Leetcode 543 - Python

  Рет қаралды 33,616

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 31
@NeetCodeIO
@NeetCodeIO 4 ай бұрын
I've already made a solution for this problem but i thought it could be improved so i rerecorded it. If you notice any other problems in the NeetCode 150 (neetcode.io/practice) where the video solution could be improved, please let me know!
@floatingpoint7629
@floatingpoint7629 4 ай бұрын
thanks for this, the explanation is much better then the one before 👍
@jeysiva8367
@jeysiva8367 4 ай бұрын
This explanation is better and clear, Thanks
@chronobytes971
@chronobytes971 3 ай бұрын
Much better than the previous one! I have understood it at once. Thank you very much
@jrose2082
@jrose2082 2 ай бұрын
you’re awesome dude
@dzccccc
@dzccccc Ай бұрын
Thank you for everything you do :)
@galkk3
@galkk3 3 ай бұрын
another option that I prefer to use regarding the res variable is to declare it as an array like res = [0] and then any time I want to use it it is like this: res[0] = ...
@bennycai7552
@bennycai7552 25 күн бұрын
I found one thing. When you do 1 + max(left, right), what if curr doesn't have left and right children? The height should be 0, but 1 + max(left, right) will make it 1.
@DerekGomez-n1w
@DerekGomez-n1w Күн бұрын
if curr is non-null, the height is 1.
@zhalzel
@zhalzel 3 ай бұрын
Great timing this explanation is super clean!
@benmyths
@benmyths 4 ай бұрын
brother just now i started solving this problem and i see your latest video on it. W timing haha!
@davitmodebadze9707
@davitmodebadze9707 4 ай бұрын
For a second I thought leetcode posted the daily problem earlier than usual
@mdrashedulhasanmr8456
@mdrashedulhasanmr8456 4 ай бұрын
it made me recheck leetcode, twice
@mohanedomer9081
@mohanedomer9081 4 ай бұрын
same lmao
@juanmacias5922
@juanmacias5922 4 ай бұрын
now I'll be really scared if this turns out to be the daily HAHA
@michaelroditis1952
@michaelroditis1952 4 ай бұрын
Me 2 got confused haha
@DerekGomez-n1w
@DerekGomez-n1w Күн бұрын
if you're confused by the return statement remember: height = 1 + max(left, right) diameter = left + right
@NikithM-vj6pn
@NikithM-vj6pn 4 ай бұрын
I know it is a big thing to ask , but can you make short videos for all the 150 neetcode problems , suggesting the most important parts / techniques / approaches required to solve the problem? Huge thanks for your short videos they are helping for quick revision :)
@NikithM-vj6pn
@NikithM-vj6pn 4 ай бұрын
could you do reverse linked list nodes in pairs of k?
@LuisSolorzano-p9u
@LuisSolorzano-p9u 2 ай бұрын
Hey thanks for the video! When I was doing this problem I knew that I could use a global variable, but in bigger coding applications global variables should be avoided. Do you believe using global variables for interview sessions is okay? Is it too much to ask an interviewer if I'm allowed to use them rather than assume I can use them? Thanks!
@dzccccc
@dzccccc Ай бұрын
A bit late but I saw either a video or podcast with Neetcode where he mentioned he passed the variable as an argument instead of using global variable, and his interviewer was asking him how he could simplify it, and he said he believed that they wanted him to use a global variable to simplify it
@rryt000
@rryt000 2 ай бұрын
can someone explain why the space complexity would be O(h)?
@hi-ew2ox
@hi-ew2ox 2 ай бұрын
in this case since we're using recursion, the space complexity is based on the maximum number of nodes we're storing in the callstack. this would be equal to the height of the tree, since that is when you have reached the deepest part of the tree from the root. in the worst case, if the tree is completely vertical and all the nodes are just stacked on top of each other, the space complexity would be O(N) where N is the number of nodes. in the best case, it would be O(h), where h is the height, e.g. as mentioned in the video if the tree was balanced it would be O(logN) since theres a logarithmic relationship between the height and the number of notes (at level h in the balanced tree, there are 2^h nodes)
@dusvn1484
@dusvn1484 2 ай бұрын
I have same problem to understand this so I will try to help you on the shortest way. The space complexity of recursion dependent on maximum recursive calls at the moment.So as you can see on neet example maximum deep where we can be is the height of tree which value is 3 in video example. Log with base 2 of N where N is equal to 8 is 3. So we see the relation of log base 2 of 8 = height of the tree. Write me some feedback if this help you :D
@mohanedomer9081
@mohanedomer9081 4 ай бұрын
if you have the time plz do 493. Reverse Pairs . I couldn't understand it at all plz
@Emorinken
@Emorinken 2 ай бұрын
I was wondering why this had just a little over 200 likes, then i realized it was just posted a month ago
@PDSREACTION
@PDSREACTION 2 ай бұрын
if this is easy then Im mike jackson
@maanas_sehgal
@maanas_sehgal 4 ай бұрын
Ayy neetcode video at 2:30 AM India❤
@mohamedabaza9700
@mohamedabaza9700 2 ай бұрын
legend
Number of Senior Citizens - Leetcode 2678 - Python
5:07
NeetCodeIO
Рет қаралды 5 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 150 М.
Diameter of Binary Tree - Leetcode 543 - Trees (Python)
11:16
Diameter of a Binary Tree - Leetcode 543 - Python
15:34
NeetCode
Рет қаралды 237 М.
10 Important Python Concepts In 20 Minutes
18:49
Indently
Рет қаралды 313 М.
He wrote this out BY HAND? // Code Review
24:01
The Cherno
Рет қаралды 192 М.
how NASA writes space-proof code
6:03
Low Level
Рет қаралды 2,3 МЛН
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 695 М.
I never understood why you can't go faster than light - until now!
16:40
FloatHeadPhysics
Рет қаралды 4 МЛН
Where is Rust being used?
11:46
Let's Get Rusty
Рет қаралды 126 М.