1. Home
  2. Explore
  3. Computer Science
  4. Data Structures and Algorithms

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Data Structures and Algorithms

Transcript

Oh welcome guys I think we are live now welcome again to the second session of data structures and algorithm before I start let me quickly introduce myself so this is the Shah Mohan I have close to like you know nine years of industry experience out from IIT bhu and doing this like nine you have time I have worked in the different pillars of society domains for example I have worked for the database child I work for e-commerce company I have worked for a PayPal which is again a giant in terms

of payment domain and currently I work for the LinkedIn in a capacity of a senior engineer right so I think the great learning has brought a great initiative to bring us on this platform and you know bring this sessions which will help you to go through the nuances of the data structures and algorithms so as a part of this whole course and so just the beginning I just try to reintroduce you know what this course is all about so through all these consecutive session I will be having session like three times in a week will be Monday and Saturday most probably during this session we'll be talking about you know the basic foundations which is needed to learn such an algorithm we will try to cover the whole linear data structure so in the last session we talked about the time proxy then today we will be talking about the recursion then we'll talk a little bit about arrays you know sorting algorithms linkless you know stack you so everything that it takes to build a

strong foundation of the data structures and algorithms this is what we will be covering in these sessions and mostly covering the linear data social aspect of it so please feel free to join share among your friends because I think this is the three course and you can make the best out of it and I sincerely feel if you vote through it one thing is for sure that I can guarantee you that you feel more confident as a software engineer as a new learner as a person who wants to crack you know the you know data sent data structure centric interviews and at the same time you feel more connected you will understand the practical aspects of what a data such as an algorithm is said that throughout the session I also try to keep it very industry relevance I don't want to keep it just a matter of examination on accurate till aspect they do keep it very industrial no relevant topic so that you understand it how these data such as are actually get

to use in the industry so this is what it is so I think let's get started so today's session is about recursion okay so we'll try to first understand you know what recursion is why do we need recursion and how do we you to solve the problems on the recursion so once we understand that concern will also try to a few solve few problems to understand the concept well and the rule of the house is that try to keep it as interactive as possible whenever you have any doubt which is relevant to this topic feel free to ask and I'll try to take your questions as and whenever I get time and I'll try to answer you I'll try to answer most of you guys so please feel free to you know keep pushing the questions right so this is whole about so we'll try to keep it interactive we'll try to keep it as relevant to the industry as possible and we'll try to focus our you know study of the data structures and algorithms around how it will be used for solving the real life

problems for solving the problems in the industry and how it does relevant to you know from the roof of the eyes of the interviewers right so I think let's get started so me you as a student who actually you were watching this video you should also have some expectation and because we are going to spend next one hour of time reading this topic so these are the few fine points which I feel you should expect from yourself then at the end of this one-hour session you should be knowing for sure what is that you should be very clear about what is recursion okay the second thing is that how recursion is supported right for any language whether you're solving for Java or Python or Scala right in any of this language what is the underlying data structure which you know which powers recursion so that is the second thing the third thing we talked about what a different kind of three versions that we get to you know work or get to

you so this is the third type the fourth one is that that is very important because you know I have been talking to so many students like you know everybody has heard of recursion and the only problem and everybody understands it all so the only problem is thinking recursively then in this struggling up the recursion the different pockets so we try to also understand how to think recursively right and the fifth one is that and then we turn this I think you should have a better confidence a better clarity to how and why to apply the recursion in the different problems now the reason I had put recursion at the starting of this session because recursion is one such strong concept which would be used again and I mean so for example we study recursion now and again soon you will realize when we are reading you know some kind of sorting algorithm it will be he use in Mirsad it will be using that quicksort then we moved to the another topics like linked

list there recursion may be used for reversing the linked list or reversing of the payer right and as we move advanced and advanced the topics like trees and graph you know the 90% of the problems on the tree and the graph you can be easily solve only if you know what recursion is the people also you know fear about dynamic program again you know the ability to solve a dynamic programming lies how good you are in recursion is right so again the recursion is something which is a base of all the foundation of all the new advancing that you are going to do in data structures an algorithm so when that point in the mind you know we have tried to put the recursion as one of the initial topics and we'll take it from here okay so I think let's get started and so let's see about what recursion is so we are talking about recursion so before going deep into what it is let us try to understand what is the literal meaning of the recursion is so if you go

and see how English dictionary and 50 I do figure you talk what is the question it will it will you know it will mean that something which is repeating again and again so I have few question which says that you know will be discussing the solution to the sanguine one yes we will be discussing the solution assignment one maybe we'll take up in the unmanned it on the last few minutes of the session we'll be talking about the assignment from the previous session there's another question as of now that can we apply these for the Python - yes try in a programming language whether it's near a spy turn or it's Scala or just Java or any other language and such

you know this concept of the recursion can be applied in your language so whatever that we study in data such as an algorithm this language neutral whatever the language is you can always go and implement it I'll be using Java as a medium but you can always feel free to go and apply in the Python and if you are finding any issues just comment on this YouTube you know whatever link that you will get in that nature will try to respond you as much as possible so whatever issue that you have any questions around that particular topic you can just post it as a you know as a comment session in that YouTube video and we can take it as a you know a chat thread or discussion thread and we take also whenever you have you given any assignments if you have any doubts you can put two doubts in that chat section in the comment section of the YouTube video and we will try to pick it from there and we can have the discussion then and there also cool so let's talk

about the regression so again coming back to the Gershon thing so as I said if you go and look into anything else dictionary what is the meaning of the recursion it means something which something which repeats itself right recursion is something which you know which repeats itself a very simple examples would be you know if you suppose you define a function and inside the function you call it again right so what is happening is that you know this is the function definition and

actually you call the function this called the function again and again this will call the function again this will call the function so it becomes a kind of circle right so this function is calling this function and this function is calling this function right so recursive recursion means you know you're calling yourself again and again and again this is what the literal meaning of the recursion is now we are trying to bring that literal meaning of the recursion into the programming world with certain constraints with a certain constant so let us try to understand you know what is the meaning of the recursion in the terms of the programming so again as I said the medium of the recursion is to do things again and again and again so it is something similar to what we do in the case of the Ford movie right so I think most of you would have already aware of some kind of syntax of the follow and if not I just give an example so far maybe something

like this suppose you have a number from you know number from 1 to 10 and you want to go and I to date to one of you want to put all of them so you want to print one then you want to print two then you want to print and then you print finally you want to print 10 so what do you do is that you normally said for I equals to you know 0 i than you is less than 10 I plus plus and you're saying that I'm starting the I from 1 and I'm going to do 10 right so we will first print 0 then we'll print 1 then 2 then 3 and something like this is going to print death so in the for loop also what you are doing is that you're trying to repeat the same thing one after the another right and basically you are doing it till the time the value of I is equal to 0 and it does become less than equal to 10 right so that is how you are writing a for loop so recursion is a return a trout that you have taken to solve the same problem that you are

trying to solve with the help of I did it evaporates okay so requesting is calling itself again and again so in the loop also we are trying to do something again and again so there is a small similarity between the recursion and the for loop so recursion is normally another round or another way of solving the for loop problem more efficiently and more intuitively now I am used to one's more intuitively and more efficiently we will see that how it makes sense when we see a particular problem so again coming back to you know what recursion is in the terms of programming language it is a kind of function which call itself you know again and again it is the kind of function which calls it again and again so when this definition has tried to create a function because it me so suppose if I have created a function supposed function as public wide and I say you know friend name okay and and this I was going print the name at system dot out dot print Ln I say hello students

okay and then I call this method again print name okay now what we see here is that this is the function which I am defined and inside the function zip and calling again you calling I mean now this pattern of a function in which you have defined a function inside that function and calling the function again this is known as the ricotta sleep pattern because pattern this is 100 Agassi pattern so this is a genetic pattern of a regression in which you define a function and inside the function you

call the function again so I defined this function in print name I am calling this function again inside this now this particular problem had some issues in it this will give us some kind of you know unwanted state can you guys write it into the comment that what is the problem with this state with this function call if you write this function called print name what is the bad impact that it will have just look into and just put that into the comments so for those so they said one listen what is

the problem with this one the problem with this one is that first of all when I call this print name function like this okay so what happens is that as soon as I call it as soon as I call it the first time the function gets called print name right it comes inside this step it prints hello students as soon as the pins hello state is again called this function printing so second time the function gets cool called this print name and it again prints hello students

right so it goes from the head to hit then again it calls hello third time it goes and calls hello students so what did he see is again it will go and said call hello students so this thing will continue happening the function we keep on call it one after the another it will not end and we keep on doing till the time it will go out of memory so in other words also I can say it will go in the infinite loop so now this is a recursive pattern but this is not the right way of writing or recursive code because if you write this way if we keep on printing hello student hello student hello Phil for an infinite amount of time and we'll keep on doing till the time it will out of a movie and it will simply crash right so there is a problem with this but now we have understood what are you got seat pattern is so the next thing is that how do we write up you know a recursive problem how do we solve a recursive problem and what is a

common pattern that we should follow when we are solving a recursive push so before I move on I have a question that you know how frequently are coefficients related to Bickersons asked in the coding interviews so Saruman who draw this question is you also I think this the recursion is something a question related to recursion will be asked in every interview because this is such a foundational concept right even if they ask you a question from the linked list that might be related to the recursion if they ask you a portion related to the sorting that might you know the underlying base will be the recursion in the ask you a question of fir tree there's 100% chance that you'll be using the question to solve it so this recursion is so important I do nobody will directly ask you a question from the recursion but any question that they ask underlying implementation will be the recursion itself so that is why this concept is very important so the

next thing that I'm going to talk about you know what what is a fee if you keep wind that happy you know keep in the brain when you're defining a recursion is very important so I hope this answered your question and let's move to know you how to write a correct recursive code let's move on then more so think Aiza recursion has a very strange way of solving the problem okay any recursive problem that you have write any recursive any recursive code that you write basically a recursion is

nothing but a function that you write in which you call the function again so any recursive function that you write it has it will have three things the first and the most important thing is not termination condition this is very important at least for in my last example it was not terminating anywhere it was just going for the infinite loop right so whenever you are writing a recursive code the first and the most important thing that you have to keep in mind is that provide the proper termination condition provide a proper termination condition right the second important part is the business logic the second important part is the provider proper business logic like whatever that you have the logic may be to just print the name the logic can be do some complex calculations so you first hit the termination condition if it is satisfying then you just return out of that function if it is not you provide some kind of business logic which that particular function is

intended to solve this is the second thing that you apply and then third thing is that you call the function recursively okay but no change input size let's try to understand these three key points using an example okay so suppose you know my problem statement is that print hello students say ten times suppose this is my problem statement okay so for that what I do let me write a function so what I do is that public why and I say print name okay and this eight parts input number so that you

know if I have to print say like you know 10 times I'll pass n is equal to 10 if I have to print like 5 times and say like N equals to 5 so now I have defined a function the function name is print name and it is taking n as the size of the input how many times I'm trying to print the name and it is not returning anything right so let's try to follow this rule of three-pointers while in defining your recursion so first of all I look for the determination condition okay so very first thing that we need to write inside any recursive function is the termination condition so can you guys help me with the termination condition you can write what should be the termination condition in this case in your YouTube chat and you can then you know cross-check with your answer whether it's going right or wrong and you know strongly encourage you guys if you are there please try to follow along with me trying to write in the code you can have a pen or paper to

write the code or you can do it you know in a parallel in your computer but I strongly encourage you guys you know whatever happens that we're solving here please try to solve in the pen and paper okay so what we do is that we have to first write down your termination condition so what we write if okay if n is less than equal to zero okay so if the N value has now become reduced to equal to zero what you do I should return don't do anything and you just return it so this is my termination condition so this will be my you know termination condition so if n value is equal to zero it means you know I don't have to print any number of time so I just return it or if and value

less than zero then it again it doesn't make sense so I just return okay the second part is at the business logic so the business logic is that I have to print me in fact to just print name so I'll print some statement so what I do I do business or thing is this oh let me do another color of the paint so much right so so I tried worried system dot out dot friend and here I'd friend you know hello my student something like this I suppose it so this is the second part that happen which is a business logic right and the third part is the third part is called the foursome recursively now since I've already printed it once here so the next thing that I am calling this function recursively print name what I can do is it I can reduce this end by one because I have already printed it once so this

is a very simple example of a recursion and which I have tried to put three things the first one is my termination condition this was fair the second one is my logic that I need you to write and the third one is the recursive call right and in the recursive are you've seen and reduced the input side initially the sides worth N and now the size is and minus one now the size is and minus one let's try to take another example and it will be very clear okay so suppose another example here is

suppose I have an array suppose one two and three and what I need to do I need to print all of these elements I need to further spray the one I need to print two and then I need to print three let's try to solve how we can recursively do so for that we will define a function so mainly you know public Y and say print array okay and this method is taking input as the array and I also say that what is my starting index suppose this I is my index okay so this is my method so

this is the recursive function that I have defined and this function should recursively print all the elements of this array guys please try to follow me okay as I said the first thing that I need to do in this case is that I have to write a termination condition right the first thing blindly in any recursive call whatever take a sit problem is you don't have to worry about the first and only thing that you need to look for is what is the termination condition okay so you guys can you know print in the comment section that what is the termination condition here and then you can check with whether it's right or not so basically the two inputs that I've taken here is the array itself and the starting index right so basically my starting index will start from here and we move Tilda in so it's starting index starting it will be 0 then it will be 1 and then it will be 2 right so once I move beyond this index I'm literally done I am literally done

processing the array right so my termination conditions would be once and done with all the indexes of the array I should simply return so for that what you need to check if so for this we can you know I know we can write the code if I is greater than equals to array dot lint now what is the array dot length array dot length will be here 3 right here my array dot length will be equals to 3 because it is it has three elements right so an index of the value I till

that time it is zero till it is time for until it is time to it has done it but if it goes beyond two which is equals to array dot length and more than that in that case I should not do anything and simply return right so this is my termination condition right the second thing that we need to do is that we have to write a logic so what is the logic here you have to preheat each of the individual I mean have to put in first one then have to strain to then have to print three right so simple way of doing is that I just do you know I write system dot out dot print Ln and I do simply hurry alright so now for the given index I have already printed 0 1 so I have intended so this is done so the next part which is left is the recursive call itself the recursive call itself right the recursive call itself so when I call it recursively right and when I try to call it recursively now what I can do is that I call this print array I

pass 30 and now since I index element has already be printed here so I need to increase it so I do it I plus 1 so this is another simple example of using recursion to print all the elements of the array in which I have so do you know I have to provide a termination condition so this one is the termination condition this is the logic that you want to pick and here you have called recursively you have called it because you believe in the increase you know index value have been increased so whenever you know the few important point to note here is that termination condition is important then you have to applied the logic and the third and the most important point is that you have to call it recursively but whenever you're calling it recursively you know this index has to be modified because you've already done with the ayat index so you should move to the next index right so I think this will make more sense if we do a dry run of it if we do a dry and or no fix to try to

let us try to see you know how we can dry run this so let's try to you know use this space from the driver so suppose you know I have suppose I have the given array as this 1 2 & 3 and suppose I call this function like this print array and I call this array here and I called starting index and you know because the starting index in the array starts with the zero right so the first time in the function gets called right so first time when the function gets called right so the first time when

the function gets called at that time this print function as the array value equals to 1 2 & 3 and the value of index is equals to 0 at that time right and then it will go inside this now if condition of my value is equal to G 0 or 8/10 is equal to you know 3 so it's just not satisfied so I will go and print or you know so what it will do it will print 1 right and after that this recursive call I mean happen soon as happens it goes to the step two now in the step two also the array value

remains same which is equal to one two three but here the value of I has become equals to one because I increase the I and then I put it right so again when it goes inside the function I value is 1 here and the array length is 3 so it will not do anything so you will again call this method and will print what it will print to right and then again the recursive call happens so it will go inside the function third time again the value of I is 1 2 3 and I value now becomes equals to 2 now again this if condition will not suspect book I use to and/or it or length in 3 so it will not do anything it will log in go go and print 3 here and again call recursively again so again 4th time the value of array is 1 2 3 and I value is now 3 right so I value now becomes equals to 3 now right now now I values 3 and array dot length is also C so this condition gets it so termination condition is satisfied and from for what I do I return ok so what I'm saying I am going from the step

one and then I am going from step two to three and then I'm going to step three to four and then I am returning but when I am returning I am not returning directly so what will happen is that after the fourth I go back to the step 3 so I come back to the step three here but then nothing is left so again I will go from the step three - I will go to the step two and then from the step to forget it is I have here so it will again go back to the step one and finally from the step on it will finally terminate so another strong point to understand here is that unlike a loop in which is the one directional motion you move from the I equal to 0 to I equals to N in the case of recursion it's a complete loop you first keep on moving down down down till you get a termination state and then again you come back to the starting position and that is a beauty of recursion this and this I want you guys to understand very clearly because you know with all my interaction you know 90% of the people

don't understand this meaning of recursion recursion always you know works in a complete loop it goes from the one end to the extreme and when it does hit the extreme it will again come back to the starting position and it will only terminate from the point where it started as you can see it calls called the first time it gets called to the second second to the third thought to the finally when it returns the termination condition it again come back to the three again come back to 2 and I get comebacks 2 1 this concept is very strong and if you understand this concept trust me guys will help you to solve many many problems very simply very easily will again repeat this concept which I am repeating it you know in a recursion unlike a normal I curative approach it is a complete loop you go from one name till the time you hit the wall that is a termination condition and when you come and then you start coming back to the standing position only when you reach the point

where you started you finally terminate the condition and this concept is very good if you know how to exploit this situation you will be able to solve the problems very simply and very easy will take up and you know the another example here and you see how that the how does this make sense so so curving that is the question that you know what is the benefit of the recursion and what is the difference will loop and the recursion so I think of this question you know just be a few more minutes we'll take few more examples and by that time you will yourself be very clear you know how strong this because the concept is and why people you don't go beyond the I Trinity when they try to look for the loops so sweetie has a question make with recursion always provide better time complexity when compared to the I creative solutions no it's not like that you know that the gushin will provide you better time complete seeds and the I

to native one but sometimes in certain situations if you try to solve the problem using idea tively right the solution will not be very intuitive if you want to solve it very intimate very simple very clear mates that time because she will make motion so we have an example and we will try to solve it and I think that will make your this concept much clearer yes between so we will be talking about this complete loop engage and another example and I think that will make you concepts clear so

let's take another example we'll try to just extend this one so suppose I have two problems to solve a half okay and this part of the code I will use for writing the code and here I'll use for the driver so this is a dry run section this is a dry run section okay and above it so suppose I have an array 1 2 & 3 D and I have to actually print all the array in the same order and I have the same problem array 1 2 3 and suppose I have to print it the reverse order okay so same similar problem but two

different requirements I have a given a same array and in the first case I have to print in the same order 1 2 3 and in another case I have to plate 3 2 1 ok ok so for the first part we have already seen the code we have already seen the port so let's try to quickly write the code of it so it does you know public wide and then say print and I pass array

so he was gave us in today and in index okay and for here also I am also given the same so here also for example I have been given the same methods definition public why green end and end I write and this is my method and this is a matter so exact same method definition exact same method efficient the name of the method is also same the arguments are also same but in this I have to you know apply the logic in such a way that I print one first to second and three last and this

I have to print three first to second and one last okay and you'll see that you know how with the beauty of the recursion and the last statement that I said you know the gerson works in a complete loop how that concept if you use the same function we can get the two different prints right so let's try adding the core the first one I have a written so I will just quickly complete the code of it so first of all we write to the first thing that will idea the termination condition the term additional condition will be what if array dot no length well you greater than equal to Y so maybe if the value of I is greater than equal to or a dot length I do nothing but I simply return so this is what happen so this is my termination condition then I have to write the logic so the logical system dot out dot print Ln and it prints array I okay and then I call the recursively again print and I called array and I call I plus one okay and if I do this it will print me one

two three let's try to see quickly again doing a dragon of the same thing because I feel that this is something which you should you know pay great attention to so if you missed you know understanding the concept in the last question please understand how I am trying to do it right okay so first of all when the first end the function gets called okay and that time you know we are calling the function like this print array and 0 and this is how we are calling the function first and the value of the array is 1 2 & 3 and the value of index is equal to the I 0 right you only get all of you agrees so as soon as it happens it can decide to check if the if condition is valid or not if condition is already married because I adored length is 3 and I when you will 0 which is satisfied so go and bring this REI so I go and print one right and then it goes then again this function versus recursively called so far second and the function gets

called the value of I remains same which is one two three and I became very equals to one because I incremented it when I am calling it right so again you'd come inside this and I pause so when it comes to one step one two it again prints two and it goes to the step three in the step three eigen value of array is one two three itself and you I have no interest to do it will print three here and it will go further down step four and here it is on two three I value has now equals to three and when I value table three this condition gets true and here now it returns and this is what I mean to say that you know it is a termination condition or when it does in the work now as soon as it is done it will again try to come back so from the step four feet again comes back to step three so those guys who are a confusion that I am stealing it is a complete loop try to understand this so I'm coming back from the step four to the custom three here right again when I come back

to the step three I am here after this nothing has to be done I am already at the last so again I go by from the step three to the step two and again I go from madness step two to the step one and then I finally done so since I am going back to the step four and then I again coming back to the step four and then finally returning so that is why I am calling it is a complete loop and in this question when I follow many write the code like this when I write the code like this this will print me the answer in this form it now using this concept of the nikasha although the hint that I have given you that you know recursion happens in a circular loop it goes to the end and it again comes back can you guys help me understand what is somewhat changed should we do in this logic that this no ice with the same method name I start printing it in the reverse order start printing me in the divorce or not so you can try it with your pen and

paper and if you have any solution you can put it on the YouTube comments we try to solve it so again the termination condition remains the same right so the termination condition remains the same so it will be if array dot length as equals to I then I simply return the termination condition remains same because once we are done with the Llano if we are moving from this direction this direction index from zero to two and once I have reached this position it means AB you know cover all the angles

so I can simply return right now the second part is that no what no can you guys guess by now that what is the change that we needed if here's that you can just put into the comments and if not just try to follow along so the only change that we need to do is that instead of printing first and then calling it recursively what I can do is that I first call the recursive function print I plus 1 right and I do a print later on I know a print later on system dot out dot print Ellen hurry

hi so the difference between this problem and this problem is only that I have called the recursive function first and then added a bit logic and in this I added to the logic first and then I have printed and let's try to see your dry run of this and see what difference it will make right so again I'm calling this function like print rate and zero okay so the first time in the function gets called the value of or and guys please be very strong attention to us

the value of array is one two three and the index is equals to zero now as soon as I came inside this this termination condition is not satisfied and the next thing that I do is I simply and then the next step is step I call it recursively right so it will go from the step one to step two so it goes from step one to step two and again in the step two and here if you see there's no print statement and being so if you're not printing anything as of now so which are just went recursively inside that print method so now again the value of RA is one two three and the value of I has now become equals to one okay and then again when it goes equal to one this if condition is not such that because I is one two three so again this false because simply third time again the value of RA is one two three and array I when you is equals to 2 again it calls recursively with the increase value of I in the fourth time it calls for e equals

to 1 2 3 and I value is equals to 3 now now as soon as I value becomes equals to 3 its this termination condition so now it returns back its tries to return back from the step 3 - the step 4 right now when it returns from the step 3 - step four it is here right and the step three this statement has still to be executed so here the value in step three the value of I was two so what it will bring in now when it is returning it will print 3 then after printing at will again go to the step two in the step two again this print statement is left the value of ie at the second position is one so at that time it will print two and then again it will go to the one and what it will print every print one and then finally it will terminate so what we have seen here is that in this case since I have put you know the recursive call first and the business logic after the recursive call well I am returning back from the loop I am printing it so this is again a very

strong concept which I have three most of the people don't utilize it when you are using the question so you know the one key takeaway from this is that if I am a writing a recursive function the three things are important the first one is the base condition if you write a base condition and only you can terminate so that is essential that will be the first thing that you need to write in a bigger school the second thing is that how do you decide when to put you know logic first and then recursion and they are recursion first and then logic so in the case if you have to apply the logic in the same order in which the data structure is you write the logic before the recursive call so for example in this case 1 2 3 I had to print in the same order 1 first to second 3/3 then I added the logic before the recursive code but here in this case when I have to print something in the reverse order the only thing that I need to do is that add the logic after

the recursive call so what it will do is that while going down it will not do anything but when is returning back it will pick up the logic and start communicating so that way you know the same cou what you could you know reverse anything very elegantly so this is the beauty of recursion and if you use a recursion meaning of the problems like reversal and all that stuff it becomes little complicated when you're using i2 nativ approach things becomes better things become easier who I hope this makes sense please let your doubts end in the comments if you are ready so a portion from the Sanjeev in which he is saying give an example of three having recursion so tree is not a scope or I can quickly you know I can quickly give an example so for example you know if you have a tree like this one two three four five and six like and somebody tells you know can you write a pre-order for store the inorder traversal of the tree right so for that if you go for I didn't really push it

would be a little complex you have to you stack data structure you know tonight you know for loop and all that stuff but if you just use some kind of recursive thing it becomes very simple it becomes very simple so for example if you have to write a pre-order of this I can simply write you know public why reorder and I say tree node root right first of all the base condition right or the termination condition so if root equals to null if I just returned I don't do anything right but this is the

first thing the second thing is the launching that I have to write the non thing is to the pre all the fashions have to print it so what I do is that I do SOP you know do dot you get later this is my business logic and then I have to call recursively so have to bring this also recursively I have to finish this also recur absolutely so what I do is that I do free order rude God get left and I do pre-order route dot get right so basically the idea is that if you use recursion see in just

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent