Height of a Binary Tree / Maximum depth of a binary tree Algorithm [REVISITED]
Вставка
- Опубліковано 9 лис 2024
- 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.
Wow! Finaly I got someone who have deeply explained it..I got the feel...
Thanks a lot🙂
mmmmm....
Yep, he gives us the feel of ds
not like apna college
@@anandoganiya9070 both of them are good , I follow many UA-cam channel on DS and Algo because in the end it's about learning and learning .
@@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
I've never seen someone has such great patience,really good explanation!
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.
Thank you for the in-depth explanation. No other video on YT explains the recursive calls which is the most imp part of trees.
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.
thank you, this is the best most detailed explanation i found,rather than just writing the code and explaining in 5 monutes
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. धन्यवाद!
Explaining recursion is very tough and you explained it in very easy manner
I have literally searched every video related to this topic and this one cleared all my doubts , Thanking you so much Sir... 😃😃😃
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?
Looked for explanations everywhere and you were the only one that explained it clearly enough for me to understand. Thank you!
This is helped me understand it after watching other videos that didnt explain it as detailed as you. Thank you !!
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.
Exactly my thoughts
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!!
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.
1should not be added , he is wrong
Very easy and to the point explanation. Presentation is also very nice. Thank you for sharing knowledge. Looking forward the same in future.
very well explained bhaiya....hmko ni lg atha ki mai isse samjh paunga, but you really made it possible...thanks a lot..
You are doing a great work sir ,
Your tutorials are really very helpful
🙏🙏
There is an error:
for the last part:
if(left > right) {
int h = 1 + left;
}
else {
int h = 1 + right;
}
return h;
glad that somebody else noticed that
you are right, h cant be returned
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!
Your are gem sir , I saw so many video but after watching your video I got the how its working , keep posting videos :)
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.
Never thought someone can explain it so deeply... Thank you so much
In step 3 you can directly write.
h = max(left , right) + 1;
return h;
I think he meant to explain purely used conditional statement for the people to understand more easily.
You can take entire code, and put in a text box, and turn into a single line.
shorter != better.
Thank you so much !!! you have cleared all the confusion for the algorithm for searching the height of BST
Thank you Vivekanand for the clear explanation.
Thank you so much for the great explanation! Big appreciation from Japan
Can you please create playlists under your channel ? Like trees, dynamic programming, graph
Excellent explanation for recursion . Really you have a very good understanding of dsa
Very slowly explained in the start. But you made it clear! Thanks :)
You are excellent at explaining things clearly! Thank you for this video.
Thanks a lot sir, you have removed all my confusion created by my prof.
This is the best explanation i found. Thanks. Need to watch at 2x
THANK YOU VERY MUCH SIR. Your ability to express is very good.
You are most welcome
The best explanation I've seen online. Thanks very much!
appreciate you make a revisited video to cover previous doubt on height definition. good job.
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?
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 ?
Wow sir 🥰🥰....I could not hold myself to appreciate your explanation...
Thank you sir...
thankyou so much sir for such a detailed explanation
Finally someone explained it....thank you so much
Thnks sir by explaing thic concept of maxdepth and also recursion function
Very good explaination of recursion ...Thanku sir
The best explanation ever
Tremendous explainatio sir, ❤from BD
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?
Very well explained sir.. Thank you so much
Well said. Good explanation. Greetings from new york......
Cubao
Thanks Vivekanand for your videos, your algorithms helps me a lot in learning Java.
Item 😚
Thank you, I understand after seeing step by step
Thanks sir.... your clear explanations and presentation help better to understood..
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.
you are talking about the depth. Depth is 4 and height is 5.
@@Sun-yv9fr google it...
Thank you for the in-depth explanation
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.
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
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
Really good explanation, keep up the good work
sir please make the video on clone a binary tree with random pointers and also bactracking concept. your videos are very helpful for us🙂
By this i learned recursion too thanks:)
Got in one go thank you Vivekanand !
Good content! Detailed and precise explanation!
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.
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
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 :)
The best explanation!Keep posting!
best explanation on the internet
Best explanation ever!!!!
In code everything is correct but return value is -1 so we get height of binary tree is equal to 4 not 5
very nice explanation sir
Your are Awesome !!!!!!!!!!!!!!!!!!!!! You made it very simple and clear.
great explanation thank u sir
great job vivekanand....
Amazing explanation!
Thank you sir for great explanation !
Really nice explanation. ...thank you ...keep making videos
no it is not working properly....if any mistake in my code then please comment
i uploaded the code in my comment
sir could you tell me how to think to solve any problem by recursion
Step 1, base check - are you at the bottom, in this case node = null. if you are reversing a string, then its when the string length is zero. Basically this is the point when your recursion must stop. This is always the first thing you do!
Step 2, reduce the working set - for recursion to work, each time you make a recursive call you must reduce the set somehow, else you have an infinite loop. In this case the reduction is node.left or node.right, For string reverse this is when you remove one char from your string
Step 3. make your recursive call and "Assume it Works"!, that is when you call height(node.left) assume the return is the height of what you passed in. If you are reversing a String, the response is a reversed-string
Step 4. apply logic - This step varies depending on what you are trying to accomplish:
In case of height, you know you want the max, so take the max of rightHeight, leftHeight.
In the case of a string reverse . You have two parts, the part from the reduction step( the char) and the part returned from the recursion call (the reversed sub-string) - so if you took the char from the beginning of the string, you put it at the end of the reversed sub string.
Hope this helps, it helps me to keep recursion straight. - Step 4: Assume it works is the key
VERY GOOD EXPLAINATION
finally mil hi gya.... thanks a lot
thank you sir. worth to watch your videos..!!
sir return h; if and else blocks mai aayega
Thank you so much brother you are doing great job
Your videos have been very helpful
Good, I understand this very easily thank you!
what is the time complexity for this???
Very well Explained
Very good explanation.
finally found what i needed ... thank you sir
Welcome 👍
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.
OMG finally understood this bullshit, thanks so much
Osm explanation sir.....but pls little bit faster must be there
Thank you, sir! you really helped me
Hii, Can u please make video on this " Find height of a special binary tree whose leaf nodes are connected ". Thanks In Advance !!
very nice explaination sir
Best Explanation Ever
I like your explanation algorithm
Sir,You are the best!!
really good videos, looking forward to more content
Great explanation👍👍
Sir, I think there is a mistake in the declaration of variable "h" as it is declared every time in the code.
h is declared outside the function
Easy to understand 😀😊
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 ??
Thanks a ton. Clear explanation.