 What's up guys my name is Michael and welcome to my YouTube channel. Today we're gonna do another leak code grind challenge. This one is merge two binary trees. So you're given two binary trees and you want to merge these two trees into one tree. The merge rule is that if two nodes overlap you sum up the new value. The new value is that you sum up the two values of the the nodes okay. So you just sum up the two values of the nodes if they overlap right. Otherwise whichever node does not know would be the one of the new tree. So given this tree this is one this is the root one two we're gonna add up because one and two overlap we're gonna add these up one two the new tree is gonna have three three and one they're gonna add up the sum of three and one that's gonna be four two and three is gonna be five all right all right. Now we know that there's five five is here but there's nothing no five here right so it's just gonna add five to the new node okay and four is here but there's not nothing there's not nothing on this tree so it just add takes four here all right that's what they mean by whichever node is not know will be the one with the new tree and seven is here and there's nothing here on this right tree on the right side here on this right leaf you see it you see it all right there's nothing here so we add seven here okay so that's basically how you do it all right how now how do you do this please take some time to figure out how you would do this it sounds very difficult at first but the solution to this is actually very easy okay I'll give you some time to do it take one to two hours if you can't figure out in one to two hours and come back to this video and I'll explain to how to do it to you okay all right guys so how do you do this first of all we need to check if t we need to return the whichever node that is not null would be the one for the new tree okay so how do you do that well if t1 is null that means we got to return t2 do you guys understand why think about it t1 is null we return t2 because t2 is the one that is not null right if one of them is null if one of them is null return the other one that's basically what this means not no node would be the ones to use a new tree right all right now what now we got to check if t2 is null t2 is null we return t1 why because if t2 is null that means that if t2 is null t1 must be the one that does not have anything there okay at this point once this condition hits here t1 must be the one that has something inside that node okay so this is checking the other condition where t2 is null we return t1 because we're using we're trying to return the node that is not null the not no null node would be the one that is the new node of the tree okay that's what we're doing now hard part we want to merge these two trees so how do you do that well you got to call merge trees how do you merge trees you pass in the left we got to merge a left one t1's left and then we got to merge t2's left okay this is going to call the same this is recursive method call calling the same function again we're going to merge t1's left and t2's left now what are we going to do with that we also have to merge t2's left t2's right oh wait no my bad t1's right with t2's right that's basically what it means it means you're you're you are essentially merging whatever is left is going to be left whatever is right is going to be right now now now is the tricky part how which tree am I going to return though you have to basically create a new tree and then set whatever the sum of these two added up and then set the left its left node to be the merged of t1 and t2 and the right node to be the the merge of t1 and t2 the right of merge of t1 t2 but what I'm going to do is I'm going to return t1 as the merged tree all right so I'm basically going to take t2 and merge it with t1 and I'm going to return t1 as the merged tree so how do I do that I'm going to take t1's value whoops my bad t1's value I'm going to add it with t2's value okay this is going to sum up t1's value and t2's value okay so then what am I going to do I'm going to set t1's left is going to now equal to the merge of t1 and t2 and t1's right whoops is going to be the merged trees of t1's right and t2's right then I'm going to return t1 my new merged tree let's run the code something's happened what is the issue the request value is the pointer oh okay so it's a pointer so we got to use the arrow and the solution is exactly the same thing as how I expected we're going to submit it and it got accepted so if you guys understand what I was doing is basically these two conditions just returned whichever node is not if the first one's not we return this the second node right if the second one node is not we return the first node so this just ensures that whichever node we're returning is not the one that is null okay then I'm merging my second one my second tree's value with my first tree's value okay by plus equals this adds a value after that I recursively call I merged t1's left left node with t2's left tree right t1's left tree with t2's left tree merge the left part and I set that equal to my new left my I merged the trees of t1's right tree with t2's right tree and set that to equal my new right side of my tree then I return my new merged tree okay so that's basically what I did if you have any questions please shoot uh please like write down on the comment section below write a comment subscribe I'll check you guys later peace