 What's up guys, Mike the Coder here. Today we're gonna go over depth first search of a tree. So we already went over depth first search. Yeah, I made a few videos about depth first search already, but this time we're gonna go over with it on a tree. And there's really not much to it for depth first search. Ideally it's just go all the way down to one path and then once you reach a dead end, just go to the next one. Death first search isn't really that hard to understand and it's not this super duper convoluted algorithm that people need to, I don't know, memorize a ton for. Basically right here, it even says it right here. Backtracking used for traversal. Go to the deepest node is visited and then backtrack to the parent, okay? So I don't know why they give me some weird advertisements here. I'm gonna close these, I don't even need a bike. But yeah, depth first search, traversal of a graph versus a tree. So in graphs it's different because there could be cycles and all this other stuff. In a tree it's really easy because once you go down, there's just nothing there, it's null, and then you just pop back up, right? In a graph, there could be cycles and there's all this other stuff and then you could go down and come back up. And there's more than one children, right? There's more than, you could have more than two children. Whereas in trees, it's just line by line of nodes. And you don't really need to do that all that much, all right? So like, there's only three ways of depth first search and I like to do it in a certain way to, this is like a super easy thing to remember for me when I think about how to write depth first search stuff. So if you look at here, there's three ways to do depth first search on a tree and we're first gonna go over in order, pre-order, and then post-order. And in either of these, it seems difficult to understand, but it is actually not difficult. And I have like a pneumonic device that I use from basically owned this like, for whatever exam that I had in my data structures class, I use this literally this one thing and then I just got A's in all my like algorithms or in my data structures class because it is not that hard. So let's actually just, I'll open up this up in paint. I'm gonna find like another, okay. So we have in order, pre-order, and then post-order. So to do this, it is not that hard. Okay, so before we even think about these numbers, let's, I'll just explain what I use to remember this really easily. All right guys, so I'm gonna explain how you do in order traversal first and then I'm gonna explain pre-order and then I'm gonna explain post-order. So these are the pneumonic things that I use in order to do any of these tree traversals. So all of these tree traversals are depth first search, they're just like the order of your doing. These are depth first search of a tree. We're basically just going all the way down to one path and once we can't do anything, we go to another path and go all the way down there and so on and so forth until we reach the end and visited all our nodes in our tree. What do I do to remember in order? Simple, for in order traversal, we're gonna do this first. We're gonna always go to the left side, then we're gonna print the node and then we're gonna go to the right side. So for any node in this tree, your mind should be go left. If I can go left, I'm just gonna go left, okay? If I can't go left anymore, I'm gonna go to the next thing which is print the node. So this end is print the node, okay? Print the node and I'm gonna go right side, okay? If I can't go anything on the right side, then I'm done with this node and I'm gonna go back to the top and then do this whole thing over and over again. So this whole thing idea of in order is always go left. Well, if you can't go left, then just print the node and then go right, go right. And then if you can't go right, we're gonna go back to the top and then whichever side we're on, we're just gonna continue doing this. Left, node, right, all right? So now how do I do this? Let's explain this. We start at the first top of our root. Our root is one, right? This is the top of our root. At one, what are we gonna do? We're gonna go left, left, node, right, all right? So can we go left? So from one, yes, I could go left because there's two here. There's a two on the left side, right? So I went left on two. At two, can I go left? Right, remember, death first search. Always go down, all right? Always go down. At two, can I go left? Yes, there's a four here. That's why I could go down. So I'm gonna go to the left. On four, can I go to the left? No, I cannot because there's nothing here. There's nothing on the left side of four. So I can't go left anymore. So what happens now that I can't go left anymore? I have to print the node. We go to the next step. We're gonna print the node. This N stands for print the node. So what is the node right now? At four? Well, the node at four is four. So that's where we printed the node here. So as you can see in this in order, they print four here, okay? So we just printed the four, the node, which is four. So I'm gonna write it down, four. Okay, so at four, we just printed the node, right? So we're on this step, right? Print the node at four. So we already printed the node. So once we've finished printing the node, what we're gonna do, our next step is go to the right side, right? R is go to the right side. So go to the right side. Can I go to the right side? No, I cannot because there's nothing on the right side of four. So after that, we're done. At this side of four, we are basically done. So now what happens? We go back to the top of two. So at two, right? From two, we already went through all the left side of two. So that's this part. That's this part of this left side of two. So since we already went to the left side of two, that's done, okay? That's completely done. So from two at two, since we already went to the left side of two, what's our next step? We print the node of two. So what is the node of two? It's just two. So then we're gonna write two here. We're gonna print the node two, okay? Now that we print the node of two, we have to go all the way to the right side because we haven't visited anything on the right side. Can we go to the right side? Yes, five is on the right side of two. So I'm gonna go to the right side of two. At five, now I'm at five. I'm gonna repeat the same process. I have to go left, print the node, then go right. On five, can I go left? I cannot go left on five because there's nothing here. There's nothing on the left side of five. So from there on, I can't go to the left side of five anymore. So what is the next step of going to the left? The next step is to print the node of n, print the node. So we're gonna print the node of five. What is in value at our no current node of five? It's just five. So we print the node of five here. So five gets printed here. Now that we printed the node here, what is our next step? Well, after we print the node, we go to the right side. Go to the right. What is five's right side? There's nothing there on five's right side. So since there's nothing on the right side of five, we're done completely. We're done on this side of five. So now we're gonna come back up to the top of two. Now that we're done on the top of two, right? That means we've already visited all of the left side of one, right? So remember, we went going all way back to the top, right? From one, we went to the left side first, right? And now since we visited all the left subtree of one, we're done with the left side because all the values of the nodes on the left side are visited, okay? Now, since all the values of the node on the left side is visited, what can we do? We have to go to the next step. So what's after left? After left is print the node. So what is the value of our current node so far? Our current node so far is one. So we're gonna print the node of one. One gets printed. So now we're done with one. We visited one that's done. So what is the next step after we print the node? We go to the right side, the right side. So what is the right side of one? Can I go to the right side of one? Yes, I can go to three. Three was not visited yet. So I'm gonna go to the right side of one. Now we're at three. For three, we repeat the same process. Go left. If I could go left, I'm gonna go left. Then I'm gonna print the node and then go right. At three, can I go left? Remember, remember this whole notion. Left, node, right. At three, can I go left? I cannot go left on three because there's nothing there on the left side of three. Okay? So because there's nothing there on the left side of three, I can't go left anymore. So I have to go to the next step. What is the next step? N, print the node. What is my value at my current node so far? It's three. So I'm gonna print the node of three. Now that three is printed the node, we're gonna go to the next step. What's after printing the node? It's to go to the right side. What is on my right side of three? Nothing. I can't go to the right side anymore. So I'm done. After this, this whole tree is all visited, right? Every single value here is already visited. So we're done. So now our final output is four, two, five, one, three. And that's the exact same output of in-order traversal. So in conclusion for in-order traversal, always go left, print the node, go right. So no matter what node we're on, we're gonna go left if we can. If we can't go left, we're gonna print the node. Once we're done printing the node, we're gonna go right. If we can't go right, we're done on the left side of the tree, and then we go back to the top and then print the node, then go right, and then we repeat this process. Left, node, right. Left, node, right. And if we were to look at the code for in-order, this is the code for in-order, okay? Print in-order. If it's null, we return. But what we're gonna do is recursively call on the left side, right? Recursively go on the left if we can. Recursively go on the left. Once we're done with the left, we're gonna print the node. See out node data. Print the node of the data. Once we've done printing the node of the data, we go right. So we're gonna recursively call on the right side and print the node on the right side. So we're gonna call this function again, our same function, and go to the right side. That is in-order traversal. Now I'm gonna explain the other traversals, which is pre-order and post-order. So let's go over pre-order and post-order. So for pre-order, it is exactly, is very similar to what we just done, but instead we're gonna print the node first and then we're gonna go left and right. So we're gonna print the node first and then go left. Then go right. If we can't go left, then we have to go right, right? So pre-order, we start at the first root of one. So we're at one. What am I gonna do? I'm gonna print the node first. So this end means print the node. So at one, what is the data at one? It's one, so I'm gonna print the node of one. So this, the value of one is done, right? We visited one. Now, what am I gonna do? I'm gonna go left. So I'm gonna go to left side. So I went to the left side of one. Now I'm on the left side of one, which is two. So I'm on this node of two. What do I do? I do exactly the same thing. Print the node, go left. If I can, if I can't, I'm gonna go right. So at two, what is the value of two, right? I have to print the node of two. So what is the value of two? It's two, so I print the node of two. So now I've done printing the node of two, which is printing the node. What do I do? I go to then my next step, go left. So what is on my left side of two? Four, can I go left? Yes, because there's something on the left side of two. So I'm gonna go left. I went left side of two. Now I'm at four. At four, I'm gonna repeat the same process. Print the node, go left, then go right. So at four. What is the value of the node at four? It's just four, so I'm gonna print the node. I print the node of four. Four is printed out. Now what do I do? Can I go left? I have to go left. Is there anything on my left side? Nothing, there's nothing on my left side, so I can't do that. Now, since I can't go left side, what do I do? What is my next step? It's to go to the right side. Is there anything on the right side? There's nothing, there's nothing on the right side. So we're done on this side. This whole part of this is done. Now we come back to the top of this. We come back to the top of this. So this whole part, left side, was after printing the node of two. So once we come back to the top of here, this whole fourth part was the left side of two. So we already visited the left side of two. So what is our next step? It's to go to the right side, right? Go to the right side. What is my right side? Can I go to the right side? Yes, five is the right side. So we're gonna go to the right side. At five, what do I do now? I do the exact same thing. Print the node, go left. If you can't go left, go right. So at five, what is the value of the node at five? It's just five, so I print the node of five. At five. Now I'm done with five. What is my next step? Go left. Can I go left here? No, there's nothing here on the left side of five, okay? So what do I do now? If there's nothing on the left side of five, I'm done with left side, I'm gonna go right. Is there anything on the right side of five? Nothing, there's nothing on the right side of five. So now there's nothing on the right side of five. We're done, okay? We're done on the right side. This right subtree is already visited. All the values, all the right subtree is already visited of two, right? Two's right side is already visited, so that's done. So now, once two's right side is already visited, we go back to the top. Now we're at one. So at one, remember, what did we do all the way back to the top of one, right? Well, in the first part of this pre-order traversal, right, we went node, we printed the node first and then we recursively visited on left side until everything was done. So this whole left side is already visited. This whole left side is already visited, so that's this part. So now that this whole left side is already visited, what is our next step? Go right. So now, at here, at one, what is my value of my right side? Three, right? So can I go to three? Yes, because it's not an all, right? So I'm gonna go right. I'm gonna go right. At three, I'm gonna repeat the process. Print the node, go left, then go right. At three, what is the value of my node at three? It's just three, so I print the node of three. At three, I have to go to my left side. Is there anything on the left? There's nothing on the left side of three, so I leave it there, right? I can't go to the left side. So what do I do? I now have to go to the right side of three. Can I go to the right side of three? On three, right side, there's nothing on the right side of three. So I'm done. So we come back to the top of one, that means we visited the right side, all the values of the right side of one, right? We've visited all the values of the right side of one. So three is visited, right? And then all these values are already visited. Can I visit anything left? Nope, nope. So we're completely done at one and we're done. So our final answer is one, two, four, five, three, and that's this. One, two, four, five, three, and that's the pre-order traversal. Now we're gonna go over post-order traversal. Before we go over to the last part of post-order, let's go over the code of pre-order. For pre-order, remember, print the node, then recursively go to the left side, and then recursively go on the right side, okay? And then our base case is if we reach null, that means we can't go anywhere, we're just gonna return. So this is the code regarding printing pre-order, okay? So now we're gonna go over post-order. So what is post-order? Very similar to what we did so far, okay? Go to the left side if you can. Always go to the left side, okay? Once we're done with left side, we're gonna go to the right side, okay? We're gonna go to the right side. Once we're done with the right side, we're gonna print the node finally, okay? We're gonna print the node finally, and then we're done. So we're gonna repeat this process. Left, right, node, that's post-order. Left, right, node. So I start at my first value of one. What is my left side, right? At one, I have to go left, right, and node. So from one, what is my left? Left is two, so I go to two. So now I'm at two, okay? At two, what is my left, right? Can I go left? Yes, I could go to four. So I'm gonna go to four. At four, can I go left? Nope, I can't go left on four, so I'm done with that. What is my next step after left? Go right. Can I go right of four? No, I can't go right of four. So that's done with that. What is the next step after right? Print the node. What is the value of the node at four? Four, so we're just print four, okay? So that's done with that. So since we're done with four, we come back to the top of two. At two, right? We already went to the left side of two, right? So everything on the left subtree of two is already visited, so we're done with the left side. So what do we do now that we're done with the left side? Our next step is to go right. So what is right of two? Five, so I'm gonna recursively go right of two. So now I'm at five, right? At five, what do I do? I repeat the process. Go left, right, and then print the node. At five, can I go left? No, there's nothing on the left side of five. So there's that. Can I go right? On five, can I go right? No, there's nothing on the right side of five. So I can't go right. All right, so there's that. Five's right is done, okay? So I can't go right. Now, I can't go right. So what is the next step? Print the node. What is the node of five? It's five, so I just print the node of five. Five's node is done, okay? So now we go back to the top. So now we already visited the left side of two and we visited the right side of two. So what do we do now that we finished visiting the left and the right side of two? We have to print the node of our current value of two, right? We have to go to the next step, we're just printing the node over two. So what is the current value of two? At two is just two, so I print the node of two. So yeah, now that we're done with that, right? We've already visited all the left side of the value of the root of the node of one, right? We visited all the values of the left side of one. So now we have to visit, we have to ask ourselves, can we visit all the values of the right side now, the next step after left? So we're done with visiting all the values of one, left side of one, so we're gonna go to the right. So can I visit the right side of one? Yes, because I could go right here. So I'm at right side of one. What is on the right side of one? Right, it's three, so I'm gonna visit three. At three, what do I do now? I repeat the process, left, right, node. Can I go left? There's nothing on the left side of three, so we're done with that. Can I go right? There's nothing on the right side of three, so we're done with that. So now what? Now that we're, we can't go right of three. Well, since after right, it's print the node. What is the value of the node at three? It's just three, so we're gonna print the node at three. Now, now, all the values of the right side of one are visited. So this, all the values of the right side of one are all visited, and the values of the left side of one are all visited. So, since we were at one originally, right, all the values of the left side are all visited, so that's that. So this left side of one is done. All the values of the right side of visited is all done, so this, that, the right side of one are all done. So what do we have to do now? we have to go to the next step, which is what is the next step of, right? Is to print the node. What is the current node of one? It's at one, so we print the node of one, bam. Now one is visited, we're done. All the values of every single node in this tree is all visited. So the post order now is, our answer is four, five, two, three, one, which is the same thing as four, five, two, three, one. Okay, so there's that. And then if we look at the code, we will look at the code, right? It is right here, post order. Recursively call on the left side, recursively call on the right side, print the node. Okay, then see how print the node's data. Okay, so our function here is go left, recursively go left on the node, then recursively go right on the node, and then print the data. Okay, and then if our current node is null, right? If our current node is null, we just return it, because that means we can't do anything there. So yeah, that's a recursively call a post order. All right, yeah. And then this is just like implementing everything there. But yeah, so that's the code of depth first search travel of a tree, and now we're gonna implement all these values into each of these problems in lead code. All right guys, so no face cam now, but we're gonna go over some few problems, and then we're going to just, I don't know, just go over some problems and explain how the pattern can work in these problems, and then you'll be on your way, so yeah. So one of the problems that we're using, we're gonna use depth first search is path sum. So in here, you're given the root of a binary street, and a target sum. So we're gonna return true if the tree has a root to leaf path, such that adding all the leaves along the path equals the target sum. A leaf is one with no children. So basically what this means is that the root to leaf path, so like what they're saying is that if we're starting at this root, we're gonna go all the way down to any of these leaves. So basically any of these that have no children. So this seven has no children, right? Because there's no nodes underneath them, right? So this is a seven that has no children. And if we were to sum all the values from five plus four plus 11 plus seven, we have to see is that equal to our target sum, right? So we're given this target sum and we need to see if that equals to that. So that's what this problem is asking. Basically return true if there exists a sum where if you add all the values along the path equals target sum. So how would you do this problem? Well, the simple way is to run depth first search and then you have to keep going until you find a one that has, that is a leaf, right? So you just keep going down until you find a leaf and while you're going down, you're gonna add up all the values of the sums and you're gonna check if it equals the target sum. And if it does, we're gonna return true, right? Otherwise, we're gonna just keep going to the other leaves, right? We just keep going to like five, four, 11, five plus four plus 11 plus two, right? Cause two is another leaf that has no children, right? We're gonna go with that side and then we're also gonna go to the other sides like 13, right? So we're gonna do five plus eight plus 13, right? Cause this 13 has no children, right? And then we're gonna go to one because this one has no children, right? We're gonna have to do five plus eight plus four plus one cause this one has no children also. So basically we're gonna have to go through here, go through here, go through here and go through here, see if it has no children. Okay, and you might be wondering why don't we check four and why don't we check this four? Well, that's because this four has one child, right? It has 11 there, so it's not a leaf, it's not a leaf, there's children for four. And this four also has a child of one, so it's not a leaf. So yeah, we're not gonna check that. We have to keep all the way down. So I'm gonna explain the solution now. There's like, I'll just explain the code basically. Essentially just run depth first search and yeah, and then just keep going down. So in here, I don't know why they use the stack. Yeah, this is actually not, this is not a good solution. A good solution to actually just use recursion. So let's find someone else's solution. Yeah, so this is a good solution, okay? So bear with me, this is in Java, but essentially it's the exact same thing. So here, they're just going to have the function originally that we're given here, right? And they're going to have a helper function called DFS for depth first search and they're gonna pass in the node and the current sum. And the reason why they're doing that is that current sum is going to be like the current sum that we're adding up for every single time we're going down the leaf and we need to add those values up, right? Every time it's not a leaf, we're just gonna add the values up and then we're gonna check if it's equal to target sum when we do reach a leaf, right? When we reach a node that has no children, okay? So yeah, so yeah, so here depth first search, tree node, current sum. And here they're gonna pass in zero in the current sum when they're calling the function of depth first search here, right? So this is depth first search and they're gonna call this function in our call the helper function in has path sum. And they're gonna pass in zero as a current sum because we're starting with the sum zero and we're gonna keep going all the way down until we add things up. And then for the tree root, tree node, this is like just a current node that we're having that we're going through, right? As we're traversing down and they're gonna pass in target sum into it, right? Which is like the, no, not target sum. They're gonna pass in root. Root is gonna get passed in each time and then we're gonna keep going down and down. Okay, so now let's go through this code together. Okay, so if the node is null, we're just gonna return false. And the reason why is that because when we reach null, so let's say we're going back to this tree, right? When we reach null, which is like reach an empty node here, it's impossible for it to be the equal to the target sum, right? Because when you reach null, it means there's nothing there. So we're just gonna return false. We're gonna return false. And yeah, so we're gonna return false for that one. Now, here's an interesting part. What we're gonna do is here what we're gonna do is we're gonna check if it's a leaf, right? So if the left side is null and the right side is null, that means it's a leaf. And if it's a leaf, that means we have to add up the values, right? We're gonna add up every single value every time. And we're gonna add it up. And at this point, we have to check if it's equal to the target sum, right? So if we go back to our picture, right? When there's a leaf, let's say we're at seven, right? If there's no children here, a leaf basically has no children, right? So when we're at seven and there's no children here, at this point, we have to keep adding the current sum, right? We're gonna maintain account every time we go down. And we're gonna add the current sum, keep adding the current sum until we reach a leaf. And since we have no more children, right? We just have to check if our current sum is equal to target sum, right? The one that we're returning true if it's not, okay? So for that, what they're gonna do is we're gonna just add our current sum, which is a variable that's passed down over and over again. And we're gonna keep adding values to that current sum. And then once we reach a leaf, which means that the left side is null and the right side is null, that means that we have to check if it's equal to target sum, right? Check if our current sum is gonna equal to the target sum and then we return true. So it might seem crazy, but that's essentially what this line of code is doing, right? So this is kind of unreadable. I would not write code like this, but basically what they're doing is you have to read from right to left. For this case, we're gonna take our current sum that we're passing in every time and that we're adding values. We're gonna add to the current value, right? We have to add it to the current value. And then from here on, we're gonna check is it equal to target? So if it's equal to target, it's gonna return true. And if it's not equal to target, we're gonna return false, okay? So yeah, we're gonna return false. So now, otherwise, what we're gonna do is we're gonna go left and then we're gonna go right. So here, basically what they're doing is they're doing node and then left, right. So if you remember, node left, right, that was one of the tree traversals that we're going through, right? Node and then left, right. So what they're doing is they're checking is the sum, add to the current node value is equal to target, right, if it's a leaf. And then we're gonna go left and then we're gonna go right. All right, so we're gonna go left. We recursively go on the left side and we're gonna recursively go on the right side. And we're just gonna keep up summing that each of the values of the sum of each value over and over again. If we reach a leaf, right, if we reach a node that has no children, we're gonna check if it's equal to target and if it is, it will return true here. All right, so that returns left, that recursively go left side and pass in node's left value. And we're gonna recursively go to the right side and repeatedly call our function and pass in the node's right value. And then each time we're gonna add up our current sum with the current node's value, right? We're gonna keep adding up the current sum with our current node's value and it repeats over and over again. So now once we reach this bottom part, we're gonna have a left side and a right side. And remember this returns true or false if it returns true or false if we found a node that is equal to target, right? So if either of these are true, right? Then, so if a little left side, if we did find something on the left side, we're gonna return true, right? If we found a true value on the right side, right? If there's a target sum that's equal to target sum, right? If there's a current sum is equal to target sum, we'll have a true value on the right side. So then in the end, we're gonna return left or right. So that means that if one of these values is true, it'll return true, right? Remember this problem is asking if do we have, does there exist like a leaf that some adds up to target sum, right? So if one of them exists, then we're gonna return true. So that's why we have an or value, right? So if left side is true or right side is true, right? Then we're gonna return true. So that's what this or value is doing, right? So yeah, if the left side is true while we're adding up all the values here and if the right side is true, then yeah, then we return true. Yeah, so this is basically checking adding up all values. If any of the values adds up through a path to a leaf is equal to target sum, then we return true. So yeah, that's why we had that or value. And if we wanna like go over how this works, like we could like go over how this works. So if we were wanting to go over example here, basically what it's doing, it's going to just do five plus four plus 11 plus seven, right? And then checks it does equal to target sum, right? And if it doesn't, it's going to go five plus four plus 11 plus two, check if it equals a target sum. And if it isn't, then we're gonna do five plus eight plus 13, check if it equals a target sum. Then it does five plus eight plus four plus one and check if it equals the target sum. And if it does, if any of these cases are true or return true, so yeah, that's what has path sum is and that's how the solution to the code is. Yeah, now we're gonna go over another problem. All right guys, we're gonna go over path sum two now, which is literally exact same question, but this time we need to return all root to leaf paths where the sum of the root node values equal to target sum. So as you can see, it's literally just the same thing, but anytime we have a target sum that's equal to it, right, we gotta basically return an array that has all the values that we went to. So five plus four plus 11 plus two is equal to the target sum of 22. So yeah, five plus four plus 11 plus two. These nodes add up to 22. So we actually have to put them in an array when we return them. And then five plus eight plus four plus five, these values add up to 22 also. So they're in this output array. So the only way we need to add them up and put them into another array and then return it. So that's what this is, yeah. So if we look at the solution is literally exact same thing, the only difference is that you just have to return like an array every time. So yeah, this is so unreadable. My guy. Oh yeah, okay. I'll explain what they're doing here. Their Java solution is so unreadable, but I'll explain what they're doing. Okay. So first of all, this is the function that they have here and they're gonna have the return, the 2D vector that we're supposed to return, right? The 2D array list that we're supposed to return. So this is in Java, but we're gonna explain basically, we're returning the 2D array that has all the values that are add up to 22. So this array is one path that adds up to 22. This was another array that adds up 22. And this whole thing is a 2D array, right? That way we need to add up to equal to 22. So here in the first part of the code, what they have is, yeah, they just had the 2D array that we need to return to. And then here they're gonna have a single array called path nodes. And this path nodes, this single array is just going to be the array that they have every time. And we're just gonna add values to it every time we go down the list, okay? So every time we go down the list, we're gonna add values to our path nodes, right? And here they're gonna have a private function or help a function recurse tree. And we're gonna pass in the root, the sum, which is in this case, it's like the target sum that we need to add up to, right? Path nodes, which is like the array that each time the path, the array that we're adding our current path to, right? The single array that we're gonna have every time we traverse them, we're gonna add values to it, right? And then the paths list, which is our 2D array that has all the values, all the paths there. So that's what this is. And they're gonna pass it into this function called recurse tree. So if we look at recurse tree, which is this helper function, very similar to the same coding interview pattern that we had before, right? Basically, we're gonna go node and then left to right. Node left, right, all right. Node left, right. This is a different thing, okay? So the difference is that, yeah, so if our current node is null, we're gonna return and basically, we can't do anything, right? Cause we reached leave. But yeah, otherwise we're gonna add a, we're gonna add our current value, our current node into our path nodes, right? Our path nodes are right. Now here, we're gonna check if it's a leaf. So if both sides are, yeah, yeah, okay. So if both sides are null, right, that which is a leaf. So if both the left side and the right side is null, right here, so let's say we're at seven, right? So both the left child and the right child are both null. So now at this point, we need to check if it's equal to target, right? If our current sum is equal to target. So yeah, so here, they have here, is that if the left side is equal to null and the right side is equal to null, right? That means it's a leaf, right? There's a, we reached the, all the way to the bottom and there's nothing else we could go to, right? So then, if basically if the, our current sum, right? Our current sum that we're adding every single time, if it's equal to the nodes value, so basically if equals are remaining some, then we need to add it to, we create a new list and we're gonna add path nodes to it, right? So we're gonna, basically, path nodes was, is this list of integers, right? And we're going to add it, this little list into paths list, into our 2D vector, into our 2D vector. That's what we're gonna do, okay? So we're gonna add the list that we have here, path nodes of our current path that we're going down into our 2D vector paths list. And the reason why we're doing that is because they're asking all the different types of paths to go down to it. So we need to add the current value to it, all right? Otherwise, what we're gonna do is, we're gonna recursively go on the left side and when we go on left, we're gonna subtract remaining some from our current nodes value. So we're gonna subtract remaining some from the current nodes value and we're gonna repeatedly pass that into remaining some. So in the end, yeah. So here we had target sum, right? And then remember, we had target sum before and then we're gonna keep subtracting our current nodes value every single time. So then in the end, if we do reach a value where it's equal to it, we're gonna add our new 2D vector values to it, right? So yeah, we have to decrease the sum every single time. That's what it's doing. So it's similar to how like, so you know in the previous code that we saw, they were recursively adding values until we get to the target value that we need. In this case, I don't know why they're doing it this way, but this time, they're basically subtracting values every time you go down until you reach a value that is equal to it. And then you could add your, the paths that you have into our new 2D gray, right? So yeah, we're gonna go on the left side and do the same thing. And we're gonna go on the right side and pass in all the values on the right side. After all this happens, we need to actually remove, we need to pop the node once we're done processing all of its subtree. And the reason why we need to do that is like basically if we're, so once we're done getting all the subtrees, the last value of the node is kind of like meaningless. So you need to pop the node once we're done processing all of its subtree. That's kind of why. So that's why they removed path nodes size minus one. Okay, so that's what they do. Let me see if we could have like a C++ solution. Okay, yeah. This C++ solution is easier to read. Okay, remember exact same thing that we said earlier, right? So here vector path is our current path, which is the current nodes of values that we're going down, right? Answer, which is the 2D vector and there's passing it in root target sum path answer. Okay, so here is easier. It's literally the exact same code in C++. If the root is equal to null pointer, we return and do nothing. Otherwise we have to add our current root to our current path, right? The current path that we're going down on. If we reach a leaf, so if the left and right child are both null pointer, we need to check if the roots value is equal to the target sum. And if it is, we're gonna add our path to our answer array, our answer. Otherwise we're gonna go left side and then we're gonna subtract our target sum from roots value and recursively go on the left. And then otherwise recursively go on the right. And then we're gonna, in the end, we're gonna pop the last pop back, remove the last path from our list because that's really unneeded. Okay, because every time we go down, we have to pop the last value. That's what we have to do. Okay, so that's the gist of the function here. Yeah, that's the gist of this code. And yeah, we're gonna go to the next value of the next lead code solution now. So let's go to the next problem. All right guys, we're gonna go over two more problems and that's it, all right? Path sum, basically the same thing, but this time we're gonna return the number of paths where the sum of the values along the path equals the target sum. So this one is different because now we don't need to start or end from the root or a leaf, but it just has to go down to the child node. So in this case, let's say we had this tree right here and our target sum is equal to eight. In this case, since we're not starting from the root, like we don't have to start from the root, five plus three is equal to eight. So this is actually one path that could it take and five plus two plus one, this is another path that you could take and then negative three plus 11, that's another path you could take to get to target sum of eight because we don't have to start from the root. So in this case, we have three of these paths that we could take and we're not returning any of the paths, which is better than the last problem, but we're gonna keep a track count for it. So we have to return the number. So in this case, it returns three. Paths show eight. Yeah, so target sum is eight and then the paths, there's three paths that add up to eight and right this time we're not starting from the root or a leaf, okay, but it just has to go down. So let's just explain the solution. This is so much easy to read, okay? See, I don't understand why we had to have the whole hash thing, okay? So here we have count, the global variable count is equal to zero and the sum is gonna equal to zero, okay? So here's the solve function. If the root is equal to null, we return, otherwise we're gonna add our current sum to the root's value. If it's equal to the target sum, we increase one from the count. Then we're gonna recursively solve from the left side, pass in the left side of target sum and then we're gonna go to the right side of target sum, okay? Now we're gonna subtract sum from the root's value, right? Every single time we go down because we could have a sum that is not including the root. So let's say we're over here, let's say we have 10 plus five plus three plus two, let's say we have 10 plus five plus three plus negative two, right? So what if we had something where 10 plus five plus three plus two plus negative two, right? What if we're not including 10, right? So in this case, they also subtract the root's value from where we're currently at. So they do 10, so they check, they first do 10 plus five plus three plus negative two, right? But then we're all gonna subtract 10 also in case if we need to do something with that also. And let's say we're starting at five, right? Let's say we had five plus three plus negative two, then they also subtract five from it in case of three plus negative two is equal to our target sum of eight. So that's why they subtract that from the target sum as well. So yeah, that's what they do here. Sum minus equal root's value. And here what they do is they call solve with the target sum and with that. And then now they're gonna recursively call path sum, we're gonna recursively call this function. Again, on the left side and the right side and the reason why we do that is in case if we had like the whole tree within a tree, right? We had that whole sum path that is within a path. Like let's say we had like 10 plus five plus three plus negative two, right? And we had five plus three, which counts as equal to target sum eight. In that case, then we have to recursively call multiple times as well, right? We have to recursively call multiple times in this case as well. So they go through path sum, passing left side and they call path sum with the right side, passing in target sum. So yeah, that's what they do. And they recursively call this path sum over and over again. And yeah, and they call solve as well. They call solve. So yeah, yeah. And then in the end, they just return the count and because count is a global variable, that's what they have. And they have a global variable of target sum. So yeah, this is a way easier solution to understand. And yeah, that's what they do here. Okay, now we're gonna go over the last problem and we'll be on our way. As last problem and then we're done, okay? So some root to leave numbers. You're given the root of a binary tree containing digits from zero to nine only. And basically, whenever we go down the path, each path, we're just going to make it a number add the digit to become a number. So if I went to one, two and three, or let's say I did one, two, okay. Let's say I went one, two and three in this case. One, two and three here. It's gonna become the number one, two, three. Okay, one, two, three. So we're not actually like, I don't know, we're not actually gonna do the, we're not going one, then two, then three. We're not adding values, right? We're not summing them up. We're just gonna append them to like the end, the digit to the end of the number. So one, two, three becomes one, two, three. So now we're gonna return the sum of all root to leave numbers and then add them. So yeah, in this case, we have one, two. So let's say we go one, two, right? Then we're just gonna make it into the number 12. And then the next number is one, three, right? The root to leave node is one, three. So we'll make that 13. And then if we add up 12 plus 13, we get 25, right? We gotta sum all these paths up, okay. So in this case, let's say we had four, nine and five. Right, we have this path going down here, then it becomes four, nine, five. And let's say we had four, nine, one, right? So we represent number four, nine, one. So that becomes that. All right, four, nine, one. And then we have four, zero. We're representative of number 40. All right, now we're just gonna add these numbers, four, nine, five, four, nine, one and 40 together. And then we get 1026. All right, so basically we're gonna run depth first search every time. The easiest way to do this is actually just to, every time we have like a number here, a digit, we're gonna just append it to a string. So that way that we have like each of the strings four, nine, five, four, nine, one, 40. And then we could just convert the string into a number. So we have four, nine, five, so on and so forth, right? We have four, nine, five. The string becomes a number and then we could just add them up, all of them. Yeah, so that's what I think this solution should be. So have the answer, which is the sum that we're all gonna do. We're gonna recursively call a helper function solve and then we're passing in the root and our sum answer and then zero for the current value of X currently. Okay, reject current sum, I think. I think that's a current sum. Yeah, I think that's a current sum, yeah. And then we're gonna return the answer. Okay, so we pass and solve. So what is in solve? If the root is null, we return, right? So this one is no left, right? Exactly the same thing in the original video. This is the math equation they use. We take the current number, we multiply by 10 and then we add the root's value. So that's something you could do here. So let's say I was at like, let's say I was at four, at four, right? And when I went to the left side. So I went four and then went to the left side nine, right? So what I did was I took this four and I'm gonna multiply by 10. So it becomes 40 and I plus nine to become 49, right? And then from here on, when I go down, let's say I go to the five, right? I'm gonna do 49 times 10, which is gonna get me 490 and I'm gonna add five. So that gets me 495, okay? So that's what they do here. I personally think it's better to just append it into a string and then you could do like a string manipulation just change it to an integer using like two integer or something integer underscore. But yeah, that's what they did. Anyway, what was the value that, I'm trying to find the solution again? Or is it this one? Yeah, okay. So yeah, if it were to null, return, otherwise we're gonna multiply by 10, our current value by 10, and then we're gonna add the current node's roots value, right, the current root value. We're gonna recursively go on the left side, pass on left and the answer and our current value of X. Recursively go on the right side, left answer and current value of X. So this current value of X is just like the current sum. That's what we're doing, okay? Every time we're gonna recursively pass on the current sum. If the left side and the right side is equal to null, that means we've finally hit the leaf node, right? And in the end, we're gonna add our current sum to our total sum of answer, okay? And then in the end, we're gonna return answer. So yeah, that's what they do. Here they have a, remember we have to add, you should, so as we're calculating, we have like the 495, right? That's the current sum and then we have 491. So what they're doing is they're gonna add up 495 plus 491 and then we're gonna have that number and then we're gonna add up 40 and then we have 1026, okay? And that's when we reach a leaf node. So when a leaf node is when the left side is null and the right side is null, so that's like when five is null, not, and then we're gonna add it to our total sum. We're gonna add our current sum to our total sum and we do the same thing for one and the same thing for zero. So yeah, that's when we go down. Okay, I hope you guys enjoyed this video. Rate, comment, subscribe. Check you guys later, peace.