 What's up guys? My name is Michael and welcome to my YouTube channel. Today, we are going to go over this hacker rank top view. So this is a question that's super ambiguous and doesn't really make any sense. I don't even know why it's on the easy category. Basically, we're given the point of a binary tree. We need to we need to print the top view. And they don't tell you anything about this. So if you were just to look at this tree, we have one, two, five, three, six, and four. The top view is one, two, five, and six. And you would think that it's just traversing to the right. But that's not what it's doing. So I'm going to explain what they mean by top view, because this is kind of ridiculously hard to understand. So given their test cases, right, given their test cases, hold up, I just erased this, given their test cases. This is what they mean by top view. So this is your test case of their tree one, two, five, three, six, and four. What they mean is if you are to draw a line from the top all the way to the bottom for every single node you see, right, just draw a line here. What is the first node that you encounter every single time? Well, the answer is simple, the first know that you see drawing a line from top to bottom, viewing it from the top node, right for you for you in this way is going to be one, two, five, and six. Okay, because that that's where it is from the top node, three and four, you don't see it viewing from the top. So that's why it is like that. So here the answer would be one, two, five, and six. Okay, so how do you do this question? So I actually had trouble doing this problem myself. So I'm just going to explain the gist of it. And then I'll go over the code. Basically, what we're going to do is we're going to run breath first search. And every time we go down a level on the right side, we're going to increase level by one. And every time we go traverse to the left side, that we see, you know, we're going to decrease the level by one. Okay, so then what's what's going to happen is that every time we insert this into a to a sorted map, the first level that we see, we're just going to print that note. Okay, like the first value that has the same level, we're going to print that note. So essentially, what we're doing is like, we're, we're just going to keep traversing down and then anything that's like has the same level. As you see, as you can see, there's like, two, three, would have like the same level here. And then five and four would have like the same level, because if you go left minus one, then plus one, you would have zero, right? From five. So that's why it would be the same level. Right. So then if we add this into a sorted map, you would see five first. Okay. So that's what they mean by this. So yeah, I'll just explain the code now. So here we have a queue that that's going to represent a level pair level and then the current node. And we're going to push the pair of zero and the root. So what this is doing is that zero is starting as the root has the level of zero. Okay. The root has a level zero. That's where we push this onto the queue. Our map is going to represent a sorted pair of map mapping the current level that you're at. And then the node first node that you see. So then when we loop through inside the map, and we add the levels there, whichever we see first is going to be the value that we're going to print out. Okay. Okay, then what we're going to do is while the queue is not empty. So this is breath first search, we can get the front of the node, the front of the queue. If the node is not null, we're going to insert this into our map at position I. Yeah, we're going to insert answer that insert I which is going to be like the pair that we see into our map. And then we're going to push the plus one to the current level. When we go to the right side, and then minus one of the current level, we go to the left side. So we're going to push both of those onto the queue. And then what we're doing is we're just popping the queue and then we get the front of the queue again. In the end, whichever we see first inside the map is going to we're just going to print out the data at that data. So let's actually just go over like an example of this. I have this on the left side and have this on the right side. So first we're pushing the first value. So I'm screaming outside. Anyway, here's our queue. And then we're going to have our map on the bottom. So this is going to be a map. This is going to be probably hard to look at but bear with me. All right, we push the current route, which is going to be zero. And then the route, which is going to be one. Okay, so the first level starts at zero. And one is going to be the route. Okay, the date our current node. All right, we're going to get the first and first value of our queue, which is going to be zero one. Is this node empty? Is it not? Is it not equal to null? Right? So it's not equal to null. So what we're going to do is we're going to insert this into our map. So our map is going to now have a pair zero one. So it's going to have in this map, it's going to be like zero one. Okay. One is representing the data of our node. Okay, zero represents the current level. Alright, now we're going to pop this from our queue. So oh, yeah, my bad, I forgot to insert it. So once we insert zero one, we're going to insert the level plus one of the right node, and then we're going to do minus one for the left, left note. So plus one of the level, the current level zero. So we're going to plus one is going to be plus one. So it's going to be one. And then we got to push the right node. So it's going to be two. Okay, now we're going to minus one on the left side. So zero minus one is going to be negative one. So it's going to be negative one left side. And then this negative one's left side is going to be no, like, there's nothing here on the left side of one. So it's going to be no. Okay. Now we're now what it's going to do, it's going to pop the first value of our queue. And then we're going to go to the front. Alright, now we're at so yeah, now we're at this one. Now we're at 12. So we're at this node. Now is two not equal to null. It's not. So what we're going to do is we're going to push the pair of we're going to add two to our map. So our map is going to have, let me actually move this up here. Yeah. So now our map is going to have a map of one and then two as our node, right? So we insert this into our map. And then we're going to push the right side of our two, which is going to be five. And we're going to plus one to the current level of it. So one plus one, it will be two. And then we're going to push push the left side of it, which is going to be minus one of the current level we're at. So plus one minus one is going to be zero. And then second to the left here, right is null, right to the left side is null. So we're just going to be null. Okay. Alright, now we're going to pop the first value off of our queue. So this is going to be popped. And we're gonna get the front of our queue, which is going to be negative one null. So is the second one equal to no pointer? Yes, it is. So all we do is we're just going to pop it, pop this value off. And then we're going to get the front value of two and five, level two and five. So here is that slow to five. Is this is five not equal to null? So yes, it's not equal to those we're going to insert two and five to our two hour map, right? And then we're going to push the left side of five. So right now we're at five, right? We're going to push it's left side and it's right side of six. So let's push the right side of six first. So that's this part plus one. So what is the level of two? Level of five is two. So we're going to plus one to on the right side, which is going to be three. And we push six. Okay, right? So that's, that's this six, right? And then we're going to push the left side, we're just going to be two minus one, which is one and we push three. Okay, so that's this left side. So two minus one is one. Okay, now we pop two five out. So two fives popped out, we're gonna get the first value of null, which is two five zero null. And we get to the first value to the top again. Yeah, as the second one, no, it is so we just pop it and get the front of our note again, which is three six. So now we're at this part. Okay, so remember that this this left side is the level, the right side is the actual node. Alright, don't confuse this three with this level, this three, this is the level that we're at, not the actual thing. Alright, so now we're at three six is this null. No, it's not no, we're gonna insert three, six or a thing. Okay, now we're going to push its left left child and push the right child and the left child, and they're both actually negative. So it's going to be like four and no, four null. I'm actually move this up. Okay, so it's four, four null, level four, no, and then it'll be what's three minus one, it will be two. So level two, and then no. Okay, so that's pushing null. See, there's like no here and no here. That's pushing those levels. Um, yeah. Now, after we push this, we're going to pop this three, one three off. So one three is off. And then we get the front, which is going to be four, four null. And then a four null is, see, there's nothing, there's no here. So we're going to pop that off. And then after we pop four, no off, we get the front of the list, which is going to be I believe did we pop three six off? And then we pop that off also. Yeah, we're gonna have to know and after to know we're gonna we're gonna just pop this off. So yeah, there's nothing there because right side is null. So we're not going to do anything. So we just pop that off. So in the end, our map is going to be zero one, two, three. And then our values is going to be, we just go through and print these values, which is one, two, five, and six. And I think that's the gist of it. Yeah, one, two, five, six. So yeah, that's pretty much the whole solution of this problem. Basically, we're just going to keep pushing a pair of the left side. We're going to map the current level we're on and then the pair of the right side of the null. Running breath for search, push the level and the node that we're on. And then yeah, we're gonna insert that into a map. At the end, we print out the map and the map is just going to show the first the first node that we encounter. So that's going to be the the node that that we see from top to bottom. Yeah, hope you guys enjoy this video, rate, com, subscribe. I'll check you guys later. Peace.