Find height of a binary tree

preview_player
Показать описание
See complete series on data structures here:

In this lesson, we have written code to find height of a binary tree using a simple recursion.

Рекомендации по теме
Комментарии
Автор

Can't belive these 6years old videos are so much self explanatory and powerful, thanxx mann, you left ds and algo legacy for us

madhav
Автор

Your videos helped me understand in 10 minutes what my data structures professor couldn't help me understand in a semester. Thank you :)

xVermyy
Автор

source code for people confused:

int height (struct bstnode *root) {
if (root == NULL) {
return -1;
}
else {
int left = height (root->left);
int right = height (root->right);

int height = (left < right) ? right+1 : left+1;
return height;
}
}

sparshbohra
Автор

Recursion feels like, I am looking in a mirror at myself, who is already looking in his mirror at himself.

mayank_upadhyay_
Автор

You are here in data structure. You save us in final exam. Please keep going!

WebgaraCom
Автор

excellent video! especially the fact that for the base case (return -1) you showed how it will work for a leaf node..
here is the Python code:
def height(root):
if not root:
return -1
heightRight = height(root.right)
heightLeft = height(root.left)
return max(heightRight, heightLeft) + 1

easynow
Автор

Why did u stop making videos? Please continue. You are the one who made DS easy for me.

VarunVishwakarma
Автор

if you know the logic of height and depth..
Directly go to 3:05

qRpKsJt
Автор

Thank you so much for the videos. I've learned a lot from you.  One thing I don't understand about this is how the actual value of the height is found?  How is it returning a count of left edges and right to determine which is max?

mcirami
Автор

Awesome explanation of the recursive calls as well as the base case (exit condition) and how changing -1 and 0 in the base case will calculate the height of the tree or the number of nodes in the tree, respectively.

codysiegmann
Автор

your videos are a student can learn starting from 0 to 90% of data structure in just 3-4 days.
can u share link for source code of height of b.s.t.

chandramohanrai
Автор

oh god, you are a good teacher. Easy to understand.

tringuyenucminh
Автор

Such a great explanation! Helped me to understand balanced and unbalanced trees!

jm.
Автор

can we do the if statement as

if(node.left==null && node.right==null){
return 0;
}

I did it and it gave me errors...why did this happen?

Name-pnrf
Автор

sir your logic on how u use recursion for finding the sum is

lravikiran
Автор

Is it BINARY TREE or BINARY SEARCH
There is a lot of difference between those two
Binary tree :-Tree where each node has up to two leaves

1
/ \
2 3
Binary search tree :-Used for searching. A binary tree where the left child contains only nodes with values less than the parent node, and where the right child only contains nodes with values greater than or equal to the parent.

Hope that this is helpful to anybody :)

krishnasai
Автор

somebody reply please
dont we have to make a count here !!
like we have to increment the count each time we parse right !

rashmih
Автор

int maxDepth(struct node* node)
{
if (node==NULL)
return -1;
else
{
/* compute the depth of each subtree */
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);

/* use the larger one */
if (lDepth > rDepth)
return(lDepth+1);
else return(rDepth+1);
}
}


#functionInC

VISHALBHANDARE
Автор

You explain the concepts really well. Thank you!

hatsuharu
Автор

As we use in general terms,  
height - measured from bottom to top(i.e., )from ground level to how much height it went
depth - measured from top to bottom (i.e., ) from ground level to how deep it went..

vijaykari