 What's up guys my name is Michael and welcome to my YouTube channel today. We're gonna do another this is a question that the One of the comments Asked me to do which is to focus on depth first search and breath first search and I'll do that Sorry guys if I haven't made a video in Three weeks. I couldn't I didn't have enough time because of school and all this stuff But I want to try to make a video every week or every day or every week. It depends I'll see what I could do, but all right. We're gonna do this Lee Cody easy problem, which focuses on depth first search Consider all the leaves of a binary tree from left to right the values of those leaves form a leaf value sequence as you can See here. We have three five one six two seven four eight nine eight The leaf sequence is the last row which is six seven four nine eight two binary trees are leaf similar if they're The value sequence is the same so return true only if the two given trees with head nodes root one root two are leaf similar Okay, so I'm gonna assume that you guys already tried to do this problem yourself. It's eight seven two leaf similar trees on Lee code So I'm gonna start doing it right now essentially what we're trying to do is Basically any question that involves depth first That involves trees probably has to do with depth first serve depth first search or breath first search pretty much or in order traversal or In order traversal pre-order traversal or post-order traversal so most likely that whatever whatever whatever Whatever question involving trees has these certain concepts right so What we're gonna do is we're gonna get try to get the last row We're gonna use depth first search to get the last row Depth first search means you keep going until you get to the last node And then you come back up and then you keep going until you go to the next last node So in this case, we're gonna try to keep going until we get to six keep going to get seven keep going to get to four nine and eight All right, and we're gonna do that in Pretty much get all these values the last row add it into a vector or an array And then we get all the values of the second tree node, which is the second tree Which is whatever values of their last row is and then we added those values into Another vector or array and then we loop through the two vectors and arrays to compare if they're the same So that's basically how you do this I'm gonna code that right now so first we're gonna create a method called a depth first search that first search Keeping my haven't done this question yet. So I actually don't know what I'm next to expect, but Let's see what happens. Okay. This is gonna be the last row. Okay, this is there the vector that we're gonna keep adding More array that we're gonna keep adding it to until we get to the last one Okay Now we're gonna have our roots that we pass in Which is I'm gonna call a root essentially if the Root is equal to no, then that means We're past this last one. All right, we passed this. So that means that we're going to essentially return Okay, because that means that we're done Otherwise I have to recursively call this you passing in the left and the right. That's what I have to do so I'm going to check if Or Otherwise, I'll call depth first search on the left left tree passing in the root Yeah, left passing in last row and then I'm gonna call it depth first search root Left right pass in the last row Okay Pretty much then what we're gonna do is We pass in left and right and then it's gonna be or not know on whatever We check if the left root and right node and right root is equal to no then that means that We have to add it to the last last row so here I'm just gonna do a last row last row Dot push back we're gonna add roots value to it Okay Ideally this should work if it doesn't I have to do like checks if see if left or right is null But this ideally should work I'm just gonna Call depth first search on root one and then pass in a empty vector first so this is gonna be first pass this in and I'm gonna just go through this and see if it actually got the print out every value of First to see if it actually got the right value The right last the last row six seven four and nine eight. That's what I'm gonna do here So I'm gonna run the code and see what happens if it doesn't get it Worst cases in scenarios like sax overflow, but let's see. Okay. We got it syntax here. Oh, okay Wait, they're expecting a return value. So let's just return false for now for now. We love to return false Okay Hold up. Hold up. Hold up. Hold up. This is taking a long time. Why is it taking a long time? Okay, so this got me Okay, so this actually got me Three Five one six two nine eight. No, no seven four. So three five one six two nine eight seven four Okay, so this actually got this okay, so I didn't actually do breakfast search. I actually just print it out in order traversal That's why I did by accident. Uh, so I'm gonna see let me see something Three five six two seven four one nine eight. Okay Three five six two seven four nine one nine eight. Okay, so Okay, so I'm actually doing something wrong. Okay, let's see. What am I doing? Okay? push So, okay, so what we're gonna do is we want to check if the left roots left is equal to null and roots right Is equal to null Okay, and then then we add it. That's what we have to do. Okay, so you see you guys I'm learning this as well Learning this with you guys because if it's left and the right is null that means it's the last row, okay, so Both of these have to be null Because as you can see on six, there's no left or right nodes and seven There's no left and right nodes for these old left and right nodes nine. No eight. No, okay And as you can see we got seven six seven four nine eight Okay, so now we have make because we know that this works. We are going to pass in the second another pass another Array into the depth first search root to second and then once this propagates all the values into the the second vector we're going to loop through both of them First dot size they have to be the same size. I think if they're not the same size then oh boy You can't really you just know that this returns false, but Yeah, I could do that check if first size If if they're not the same number of Values, then you know, it's false. Okay, you know, they're not the same similar trees. But anyway So now we're going to check if root first at I is equal to a second at I Continue if it's not equal if it's not equal to return false Otherwise at this end case we return true and let's run the code And see if it's the same Return true return true. Let's see. What else is there anything? Okay? I'm gonna hit submit and it got accepted So that's basically how you do this question You have to keep going down until you reach the last row In this case to make sure that it's the last row We had to check if the roots left is equal to null and the roots right is equal to null And yeah, that's basically how you do it Ray com subscribe check you guys later. Let's let's first see what they did Okay, so they did exactly the same thing as I did Okay Yeah, that's what they did All right, Ray com subscribe. I'll check you guys later. Peace. Oh, you could you could check Vectors equals like that. I didn't know that You just check the vector if they're equal with equal equal, huh? Okay, let's Hold up. This this is this is a revelation Return first is equal to second. I Did not know that That's crazy. Okay, but yeah, that's all you how you do it Ray com subscribe. I'll check you guys later