Height of a Binary Tree / Maximum depth of a binary tree Algorithm [REVISITED]

  Рет қаралды 120,913

Vivekanand Khyade - Algorithm Every Day

Vivekanand Khyade - Algorithm Every Day

Күн бұрын

Find the height of a binary tree. The height of a binary tree is the maximum depth of the binary tree.The height of the root is called the height of the binary tree.

Пікірлер: 227
@codewithree930
@codewithree930 4 жыл бұрын
Wow! Finaly I got someone who have deeply explained it..I got the feel... Thanks a lot🙂
@chaminduperera3998
@chaminduperera3998 3 жыл бұрын
mmmmm....
@Man_of_Culture.
@Man_of_Culture. 3 жыл бұрын
Yep, he gives us the feel of ds
@anandoganiya9070
@anandoganiya9070 3 жыл бұрын
not like apna college
@Man_of_Culture.
@Man_of_Culture. 3 жыл бұрын
@@anandoganiya9070 both of them are good , I follow many KZbin channel on DS and Algo because in the end it's about learning and learning .
@anandoganiya9070
@anandoganiya9070 3 жыл бұрын
@@Man_of_Culture. ik but they are running which leads to misunderstanding and lots of confusion but this guy explains it just right best for beginners
@vicky888
@vicky888 3 жыл бұрын
I've never seen someone has such great patience,really good explanation!
@anupamdey4893
@anupamdey4893 3 жыл бұрын
Thank you for the in-depth explanation. No other video on YT explains the recursive calls which is the most imp part of trees.
@KeithWarter
@KeithWarter 4 жыл бұрын
Thank you for these videos! They are much appreciated!!!! This stuff needs to be slowed way down to grasp it at first just like a musical instrument. You taking the time to white board it like this is really really helpful.
@amoghgowda6117
@amoghgowda6117 3 жыл бұрын
I have seen many videos, Everyone says call the method recursively to find left height and right eight and find the max btw them. But no one told how the code is running line by line. But your the only person who showed how recursion works with 123 way of teaching, Loved the video.
@farahluvs1d
@farahluvs1d 4 жыл бұрын
thank you, this is the best most detailed explanation i found,rather than just writing the code and explaining in 5 monutes
@samyakjain8384
@samyakjain8384 5 жыл бұрын
Explaining recursion is very tough and you explained it in very easy manner
@vaishali3339
@vaishali3339 2 жыл бұрын
Was about to have a breakdown because I was not getting this problem but your video made it very easy to understand. Thank you for the step by step explanation. धन्यवाद!
@mouseclicker4955
@mouseclicker4955 3 жыл бұрын
Looked for explanations everywhere and you were the only one that explained it clearly enough for me to understand. Thank you!
@pauldaviddavies
@pauldaviddavies 3 жыл бұрын
Wow, four years ago yet explaining the concept that deep as if it is being done today. Vivekanand that is great and deep. Thank you. Where are my likes for him guys?
@love_piano_3000
@love_piano_3000 Жыл бұрын
I have literally searched every video related to this topic and this one cleared all my doubts , Thanking you so much Sir... 😃😃😃
@danielchia1389
@danielchia1389 5 жыл бұрын
This is helped me understand it after watching other videos that didnt explain it as detailed as you. Thank you !!
@xiaolongli1898
@xiaolongli1898 4 жыл бұрын
There is an error: for the last part: if(left > right) { int h = 1 + left; } else { int h = 1 + right; } return h;
@juanefrainpaquiyauri6590
@juanefrainpaquiyauri6590 4 жыл бұрын
glad that somebody else noticed that
@AbdullahKhan-pv1qz
@AbdullahKhan-pv1qz 2 жыл бұрын
you are right, h cant be returned
@rishabhjaiswal3276
@rishabhjaiswal3276 2 жыл бұрын
Your are gem sir , I saw so many video but after watching your video I got the how its working , keep posting videos :)
@viveknandan4950
@viveknandan4950 2 жыл бұрын
Never thought someone can explain it so deeply... Thank you so much
@shivamnerwal72
@shivamnerwal72 4 жыл бұрын
You are doing a great work sir , Your tutorials are really very helpful 🙏🙏
@vuramvincent721
@vuramvincent721 3 жыл бұрын
Thank you so much !!! you have cleared all the confusion for the algorithm for searching the height of BST
@VikiSangani
@VikiSangani 5 жыл бұрын
Very easy and to the point explanation. Presentation is also very nice. Thank you for sharing knowledge. Looking forward the same in future.
@kartiksuman9814
@kartiksuman9814 3 жыл бұрын
very well explained bhaiya....hmko ni lg atha ki mai isse samjh paunga, but you really made it possible...thanks a lot..
@ryujiganaha9645
@ryujiganaha9645 5 жыл бұрын
Thank you so much for the great explanation! Big appreciation from Japan
@jammy2003
@jammy2003 3 жыл бұрын
THANKS SOO MUCH!!! I had a great deal of difficulty to understand the recursion portion of this solution! You explained it very easily! Thanks again man!
@arjunsankarlal5296
@arjunsankarlal5296 3 жыл бұрын
Very slowly explained in the start. But you made it clear! Thanks :)
@piyusharmaus
@piyusharmaus 5 жыл бұрын
I really liked the way you show small details like, why we add 1 in finding the height of a binary tree, most of of the videos simply add 1 and not explain any reason behind it, i know it is easy part but at least you explained.
@harshitrathi3077
@harshitrathi3077 4 жыл бұрын
1should not be added , he is wrong
@vickyhu6970
@vickyhu6970 4 жыл бұрын
You are excellent at explaining things clearly! Thank you for this video.
@onyinyechichukwuma689
@onyinyechichukwuma689 3 жыл бұрын
The best explanation I've seen online. Thanks very much!
@ritik8879
@ritik8879 4 жыл бұрын
Excellent explanation for recursion . Really you have a very good understanding of dsa
@KamalKumar-bk6rn
@KamalKumar-bk6rn Жыл бұрын
Thank you Vivekanand for the clear explanation.
@rishipalbhatia
@rishipalbhatia 6 жыл бұрын
If you compile this code, you will get an error on the line return h; That's because the two declarations of variable h are inside the scope of if { } and else { }. You're trying to use h outside it's scope.
@shreyassule2219
@shreyassule2219 4 жыл бұрын
Hi Sir, Thank you so much for these videos. Your style of explaining algorithms with examples and then walking us through each step of the code really helps to grasp the concept very effectively. If I may be allowed for just one small suggestion can you also please explain the time complexity for these? Regardless, great fan of your teaching. Keep up the good work!!
@raunak02
@raunak02 3 жыл бұрын
Thanks a lot sir, you have removed all my confusion created by my prof.
@modernsanskari4398
@modernsanskari4398 7 жыл бұрын
In step 3 you can directly write. h = max(left , right) + 1; return h;
@yoonlee5278
@yoonlee5278 5 жыл бұрын
I think he meant to explain purely used conditional statement for the people to understand more easily.
@reyou7
@reyou7 5 жыл бұрын
You can take entire code, and put in a text box, and turn into a single line. shorter != better.
@sunitamishra5475
@sunitamishra5475 3 жыл бұрын
Finally someone explained it....thank you so much
@LarryRuane
@LarryRuane 7 жыл бұрын
Thanks, great explanation. Just one small suggestion with the code -- the variable h is declared locally within the "if" and "else" blocks, so is no longer in scope for the "return" statement. So you would need to declare h before the "if" statement.
@usman3D
@usman3D 7 жыл бұрын
Exactly my thoughts
@souravghosh7093
@souravghosh7093 4 жыл бұрын
This is the best explanation i found. Thanks. Need to watch at 2x
@mfgucluer
@mfgucluer Жыл бұрын
THANK YOU VERY MUCH SIR. Your ability to express is very good.
@vivekanandkhyade
@vivekanandkhyade Жыл бұрын
You are most welcome
@jamesqiu6715
@jamesqiu6715 7 жыл бұрын
appreciate you make a revisited video to cover previous doubt on height definition. good job.
@anjalidarokar2408
@anjalidarokar2408 4 жыл бұрын
Sir, the height of a binary tree is the number of edges in the longest path from the root to the leaf, not the number of nodes from the root to the leaf, therefore, here, height = 4.
@Sun-yv9fr
@Sun-yv9fr 4 жыл бұрын
you are talking about the depth. Depth is 4 and height is 5.
@adityasagarr
@adityasagarr 3 жыл бұрын
@@Sun-yv9fr google it...
@m.motivation6299
@m.motivation6299 2 жыл бұрын
The best explanation ever
@gokarnapandey4930
@gokarnapandey4930 3 жыл бұрын
Wow sir 🥰🥰....I could not hold myself to appreciate your explanation... Thank you sir...
@chetanjain5097
@chetanjain5097 2 жыл бұрын
You explain better than anyone else. Most videos explain logic but don't have code. You do show code. But I just haven't seen any single video that explains with test cases (with input data) and code. After viewing your video I am still now sure what value of node is passed to the function. Is it pointers to left and right edge or is it something else?
@prathameshautade2679
@prathameshautade2679 3 жыл бұрын
Really good explanation, keep up the good work
@vishnu_bhatt
@vishnu_bhatt 6 жыл бұрын
C++ code: int height(Node* root) { if(root==NULL || root->left==NULL && root->right==NULL) { return 0; } int left = height(root->left); int right = height(root->right); if(left>right) { return left+1; } else { return right+1; } } The height of a node is the number of edges on the longest path between that node and a leaf. The level of a node is defined as: 1 + the number of edges between the node and the root.
@prinsedits
@prinsedits 2 жыл бұрын
thankyou so much sir for such a detailed explanation
@abdelrhmanahmed1378
@abdelrhmanahmed1378 4 жыл бұрын
int find_height(BstNode* root) { if (root == NULL) return -1; else return max(find_height(root->left), find_height(root->right)) + 1; } you need to return -1 ,because last recursion on the leaf node must return 0 ,because the height of the leaf node is 0 so max(-1,-1) +1 --> will evaluate to 0 and for those who are intelligence enough will realize that when we return to the root node after we finish the left recursion the height will be shorter by one ,that one will get added after we finish the right sub-tree if the right sub-tree is shorter that the left so that one that get added will get the us right height ,and vice versa if the right height is longer
@abdelrhmanahmed1378
@abdelrhmanahmed1378 4 жыл бұрын
and by the way the definition of tree height is the max edges from the root to the longest path to the leaf ,so its not 4 or 5 there is only one right answer which is 4 for this example
@RN-jo8zt
@RN-jo8zt 11 ай бұрын
Def: The height of a node in a binary tree is the largest number of edges in a path from a leaf node to a target node but why we still adding 1?
@vijaysagoo2464
@vijaysagoo2464 3 жыл бұрын
Thank you for the in-depth explanation
@mdanishossain026
@mdanishossain026 2 жыл бұрын
Tremendous explainatio sir, ❤from BD
@bryanlee5522
@bryanlee5522 2 жыл бұрын
Thank you, I understand after seeing step by step
@juhisingh4509
@juhisingh4509 4 жыл бұрын
Very good explaination of recursion ...Thanku sir
@nitinmonga6975
@nitinmonga6975 4 жыл бұрын
Very well explained sir.. Thank you so much
@yasirshaikhkhan6189
@yasirshaikhkhan6189 5 жыл бұрын
Thanks sir.... your clear explanations and presentation help better to understood..
@waynegreen7970
@waynegreen7970 4 жыл бұрын
Good content! Detailed and precise explanation!
@aditi_jha
@aditi_jha 3 жыл бұрын
Best explanation ever!!!!
@ShreyasGaonkar
@ShreyasGaonkar 3 жыл бұрын
Great explanation on this and the video about the diameter of a tree. Was able to solve the leetcode question on the first go. Sub'ed :)
@chauhanrachit
@chauhanrachit 6 жыл бұрын
Can you please create playlists under your channel ? Like trees, dynamic programming, graph
@preetichauhan3272
@preetichauhan3272 6 жыл бұрын
Thanks Vivekanand for your videos, your algorithms helps me a lot in learning Java.
@shivamkeshri
@shivamkeshri 3 жыл бұрын
Item 😚
@shreyasangane3822
@shreyasangane3822 4 жыл бұрын
Your are Awesome !!!!!!!!!!!!!!!!!!!!! You made it very simple and clear.
@ashishchaudhari8223
@ashishchaudhari8223 4 жыл бұрын
Thnks sir by explaing thic concept of maxdepth and also recursion function
@vivekmit
@vivekmit 5 жыл бұрын
In a nut-shell ,we are traversing the a binary tree to count the level at each node. Can you brief we, which kind of binary traversal we implemented in this video ? Can we implement same logic with different kind of traversal like inoder/preorder ?
@tumble_dont_fall
@tumble_dont_fall 4 жыл бұрын
best explanation on the internet
@prashanthchinthilla3968
@prashanthchinthilla3968 3 жыл бұрын
By this i learned recursion too thanks:)
@SICSSonamSharma
@SICSSonamSharma 3 жыл бұрын
Amazing explanation!
@jitendragupta4661
@jitendragupta4661 4 жыл бұрын
The best explanation!Keep posting!
@JasPer22996
@JasPer22996 4 жыл бұрын
Well said. Good explanation. Greetings from new york...... Cubao
@iasaspirant1441
@iasaspirant1441 3 жыл бұрын
In code everything is correct but return value is -1 so we get height of binary tree is equal to 4 not 5
@Gudsan
@Gudsan 5 жыл бұрын
very nice explanation sir
@gauravbhagat8831
@gauravbhagat8831 3 жыл бұрын
Got in one go thank you Vivekanand !
@JULIUSNGIGIux
@JULIUSNGIGIux 4 жыл бұрын
Please update this video with the correct information. The height of a binary tree is the number of edges in the longest path from the root to the leaf, not the number of nodes from the root to the leaf.
@adarshlakshmanan
@adarshlakshmanan 6 жыл бұрын
The height of the tree is calculated wrong. Counting the edges, the height is 4, not 5. This is how you correct it: In the program, if (p==null){ return -1; //Instead of 0 as shown in the video. } This also make sense conceptually since the depth of a Null node is -1 & not 0.
@sumanreddyg8245
@sumanreddyg8245 6 жыл бұрын
So for a NULL tree what is the height you want to return -1 ? I hope 0 is right answer, if zero is right then height is 5 not 4, for NULL tree both depth and height must return 0
@2010aishwary
@2010aishwary 4 жыл бұрын
in this way the height of the binary tree with only root node will be 1 but the actual height of binary tree with only root node is 0 ??
@shilpitamedhi3204
@shilpitamedhi3204 4 жыл бұрын
great explanation thank u sir
@ArunKumar-gp2vq
@ArunKumar-gp2vq 4 жыл бұрын
Thank you so much brother you are doing great job
@mohamedelkayal8871
@mohamedelkayal8871 7 жыл бұрын
Your videos have been very helpful
@FamousEgyptboy
@FamousEgyptboy 6 жыл бұрын
Thank you sir for great explanation !
@ashaykapoor613
@ashaykapoor613 4 жыл бұрын
Thanks, buddy! It really Helped
@revanth9272
@revanth9272 3 жыл бұрын
thank you sir. worth to watch your videos..!!
@m_arslan_
@m_arslan_ 3 жыл бұрын
Thank you, sir! you really helped me
@18ajai
@18ajai 5 жыл бұрын
Very well Explained
@ashwinsingh1731
@ashwinsingh1731 4 жыл бұрын
finally found what i needed ... thank you sir
@vivekanandkhyade
@vivekanandkhyade 4 жыл бұрын
Welcome 👍
@MrRimmy
@MrRimmy 6 жыл бұрын
VERY GOOD EXPLAINATION
@maazalam66
@maazalam66 7 жыл бұрын
Really nice explanation. ...thank you ...keep making videos
@coderajput1816
@coderajput1816 7 жыл бұрын
no it is not working properly....if any mistake in my code then please comment i uploaded the code in my comment
@sagarkolhe5927
@sagarkolhe5927 5 жыл бұрын
great job vivekanand....
@100_bindusai8
@100_bindusai8 4 жыл бұрын
Easy to understand 😀😊
@ranitsinha6424
@ranitsinha6424 7 жыл бұрын
The height of this tree will be 4 not 5 .. please correct it. For more info please visit --->www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem
@itis_beard_man2471
@itis_beard_man2471 4 жыл бұрын
Good, I understand this very easily thank you!
@harshitraj4199
@harshitraj4199 4 жыл бұрын
finally mil hi gya.... thanks a lot
@amangarg6531
@amangarg6531 7 жыл бұрын
sir please make the video on clone a binary tree with random pointers and also bactracking concept. your videos are very helpful for us🙂
@deepakborah8335
@deepakborah8335 7 жыл бұрын
Very good explanation.
@shyamaharihar2425
@shyamaharihar2425 4 жыл бұрын
Thankyou so much , finally understood :)
@codelover847
@codelover847 6 жыл бұрын
Great explanation👍👍
@SahrishAli-pd8hb
@SahrishAli-pd8hb Ай бұрын
great brother habibi!
@jaysahu357
@jaysahu357 7 жыл бұрын
very nice explaination sir
@Anonymous-jt3yz
@Anonymous-jt3yz 5 жыл бұрын
Best Explanation Ever
@jeezradz
@jeezradz 4 жыл бұрын
what is the time complexity for this???
@DeepakGupta-pt5rv
@DeepakGupta-pt5rv 7 жыл бұрын
Sir,You are the best!!
@saldanhakaren1
@saldanhakaren1 6 жыл бұрын
really good videos, looking forward to more content
@anandoganiya9070
@anandoganiya9070 3 жыл бұрын
bro what to say far better than apna college
@lokeshchennamchetty1019
@lokeshchennamchetty1019 6 жыл бұрын
height doesn't include root node, so height of node is 4 not 5 ,need correction!!
@viktoriabrandstadter2333
@viktoriabrandstadter2333 7 жыл бұрын
Thx ..it is a brilliant explanation!
@srilekha9177
@srilekha9177 5 жыл бұрын
Thanks a ton. Clear explanation.
Delete a Binary Tree (all nodes in the binary tree)
8:13
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 29 М.
Diameter of a Binary Tree (Code/ Algorithm)
17:15
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 95 М.
ССЫЛКА НА ИГРУ В КОММЕНТАХ #shorts
0:36
Паша Осадчий
Рет қаралды 8 МЛН
Counter-Strike 2 - Новый кс. Cтарый я
13:10
Marmok
Рет қаралды 2,8 МЛН
진짜✅ 아님 가짜❌???
0:21
승비니 Seungbini
Рет қаралды 10 МЛН
L14. Maximum Depth in Binary Tree | Height of Binary Tree | C++ | Java
8:05
Spiral (zig-zag) level order traversal of a binary tree
14:12
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 34 М.
I Spent 100 Hours Inside The Pyramids!
21:43
MrBeast
Рет қаралды 64 МЛН
one year of studying (it was a mistake)
12:51
Jeffrey Codes
Рет қаралды 344 М.
Vertical Order Traversal of a Binary tree (Algorithm)
18:35
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 72 М.
How Dijkstra's Algorithm Works
8:31
Spanning Tree
Рет қаралды 1,4 МЛН
Level Order Traversal of a Binary Tree (level by level and as a whole)
13:54
Vivekanand Khyade - Algorithm Every Day
Рет қаралды 51 М.