 What's up guys, my name is Michael and welcome to my YouTube channel today. We're gonna go over the how to get the height of a binaries treat the height is the furthest the is the number of edges between the root that we're currently on to the furthest leaf So in this case the furthest leaf is a there are all actually balanced here So the height is just two right because it takes every time you go down a level you increase the height by one So here we actually start at zero on the top. So we have one Right to go for four to two one and then two Right all these nodes the furthest leaf are two right even if you go from four to one It's still two four to three. It's two four to five. It's two Four to seven. It's two Okay Now whereas in this situation The height of this tree is actually three because from the top of the root of three It's gonna it takes three levels to go to the furthest node, which is seven the furthest node is seven And it takes three levels. I got to take one two and then three, right? It's how the largest the furthest from the root. So that's what this height means It's the furthest level one two three. It's not this one because this is one two It's not this one because it would take three one two for four It's three to seven because it takes one two and three. It's the furthest from the node To one of its leaves So I'm just to explain how this code works. It's not that difficult Um our base case is if the root is no pointer Then we just return zero and the reason why this is the case is because at the top of the node the roots height is zero and Yeah, at this point it would just be zero right because at the top of the node you really Yeah, you're starting node is zero. So technically that's your base case All right This is a little tricky to understand, but it's not that tricky to understand actually so I create two variables called left and right And it's just gonna represent the height of the left side in the height of the right side All right. Um, we have to check if the left side exists So if the left side is not equal to the null pointer, that means that it exists, right? So if Let's say Let's say we're on four, right This left side of four is left is null right there There's nothing nothing exists on the left side. So we can't go there. So we have to check if the left side is No, it's not equal to no If we're gonna go to the left side So what do you do is if if the left side is not equal to null pointer? That means there is a left side, right? There's left There is a left Left subtree right on the left side. So if that's the case, I'm just going to get the height of left side Right. I'm just always gonna get the height of the left side or curse to be called left side Because I just I want to get the height on the left side also, right? I just keep going to the left always keep going down to the left and The thing is every time you traverse down a level, right? So if I go from three to two and two to one We increase the height by one right because it takes one step to go down. So one two, right? Every time you go down one level, it just takes one step So from three to two it takes one here three to five it takes one right so every time you go down down a level It takes one step. So that's why if left side is not equal to null pointer. I recursively call on the left It's left side of the root and I add one right because it every time you go down a level we're gonna increase by one, right? That would be the the height of it Now if the right side is not equal to null pointer that means the right side is not null Right that means there's something on the right side this part. There's something on the right side here So if that's the case what we're gonna do is we're gonna just set the right height the right height It's gonna equal to recursively calling on the right side because there's something there, right? There's something on the right side that we have to go to and Yeah, we're just gonna keep going to the right side if there's a right side, right? And then every time we go down the right side, we're gonna actually add one Reason wise because the same thing every time you go down a level So from three to five we go down one level on the right side. It's a cost of one Right, we've plus one and for five six on the right side. It's plus one also in six or seven of plus one So every time we go down the right side, we're gonna add one okay now because they say the Height of a tree is the furthest leaf, right is the one who's furthest down Right, we're gonna take the max of the left side and the right side and that's gonna get get us which one's The furthest down, right? So if I got to get the left side height and the right side's height, whichever is furthest down That'll be the height of the tree So that's why I just return max of the left side's height and the right side's height So yeah, hope you guys enjoyed this video or a calm subscribe is all that hard Pete's