let’s assume we are printing at level 3.
control at the recursion call at printKth(1 , root->left) ,
will it print root->data then return ??, now it will go back to height =2 ? , then control goes down to PrintKth(height-1, root->right) , it will go to right node and print root->data and return and not understand next of this process
How printKthLevel function works recursively
hello @amit0001
let say we want to prrint nodes at level 2.
so we make a call (2,root)
clear we are not at level that we want . so we make call to left (1,root->left) and then call to right (1,root->right).
when we go to left (1,root->left) here k is 1 that means this is the required output ,so we print and return.
again control we come back to (2,root) and then we make call to right (1,right).
here becuase level is 1, we make print and return.
okay… not understand next of this
after printing right node how it will print right subtree of top root
…
its recursion na, one the exectuion is done, control will return to calling function and next statement of calling fucntion will executee.
for example->
print(k-1,root->left) ; // here we made a call on left subtree , so left subtree will be visited and data will be printed if we get required level and once every thing is done in subtree the control will back again here.
print(k-1,root->right);// and after that this line will execute.
so it is how this ->
is happening.
watch the video again, or revise the recusion concepts for better clarity
okay…i mark as resolve.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.