 What's up guys, my name is Michael and welcome to my YouTube channel today We're gonna do another leech co-challenge or maximum depth of n-ary tree given an n-ary tree find its maximum depth The maximum depth is a number of nodes along the longest path from the root down to the farthest leaf node So in this treat in this case the maximum depth is three because the farthest Down node is five and six could because you as you could see the height of the tree would be one two Wait We hold up Yeah, okay, wait actually it would be three Because we're counting the first one as a first level so one one two three. Okay, so that's the maximum depth Because five and six are the maximum depth of the tree. Okay, so one two three. Okay, what else? That's pretty basic I'll go over the solution right now, and I hope you guys Tried to this on your own essentially if the root is null we return zero Why because the height at? Null in this case like two twos like The children of two is an all right its height is gonna be zero That's just like is it just doesn't there's no height for no node, okay Then I'm gonna create a depth value of one this is just The depth value That I'm gonna get So what I'm gonna do is I'm going to go through every single children of the root and get its max I'm gonna get its max depth. Okay, so I'm gonna go through every children of roots every children of root So for every children of root it's This is every children of root and I'm going to get max depth get each depth of every children of the All its children of root. Okay, that's what I'm doing here Now what do I need to do I need to find the maximum of The root of every single children. Okay, so that's the reason why I created in depth equal one because we know that this The smallest depth possible is one, right? I mean it could be zero, but honestly if it's not null We know the smallest possible depth is one and I'm going to essentially get the max of All the children all the children's depth. Okay, so this that's what this is doing I'm getting the maximum value of all the children of its depth So I'm calling max depth on all the for each child of root And that's going to get me a depth value for each child And and what I'm doing is I'm creating a variable called depth equal to one This is going to be the smallest possible value of possible for a Non-null root It's equal to one and I'm calling max essentially getting the maximum possible depth for every children I'm setting equal to max. So after this for loop I'm basically getting the max max value of each children's depth and depth and it's going to get me the maximum possible value for every child Every children's depth. That's what it's doing like the maximum depth for each child for a child like the maximum positive This is essentially the same thing as setting a variable Finding the largest value in an array because if I'm finding the large value of array I just set the Largest value to be the smallest one right and then I'm going to Update the largest value of the array by setting the maximum of the smallest value and the rest For in each element of the array Calling max on each element of the array in order to get the largest possible Value so it's the same thing Basically, what what I'm doing here. Okay after that. I'm just going to return depth Now there's a key issue you have to think about is that every time I go down For each child right every time I go down. I'm actually going down one more depth. Okay, so Whenever I call max depth Max depth is going to return the depth of whatever The depth of the children right but I have to add one every time because if I'm going down the child the children the children of Three is five and six right? I have to add one to the depth of three Right so in this case the depth of three is two because it's one one three and one and three right is a The because of the starting depth is it has value one right? So the height is going to be like one One two right so then the maximum depth of five is Gonna be one plus the maximum depth of three So it would be one plus two. So that's why five is three Right maximum depth of three. That's why every time I go down a Child I have to add one So that's what this is for plus one. That's on the code Okay, I got expected value Submit it and it got accepted. So that yeah, that's basically how you do this Finding the maximum depth of an nary tree. I hope you guys Understood this raycom subscribe. I'll check you guys later. Peace