Not geeting the exact output

Modified code

Your mistakes

  1. if(k==0){

     // cout<<root->data<<" ";
    
     temp.push_back(root->data);
    
     return 0;
    

    }
    add this condition in node_at_distance_k()

  2. if dr or dl !=-1 then call print function not node_at_distance_k()

  3. don’t print the required nodes because output should be in sorted order hence first store all possible answers in a vector and then print them after sorting

  4. if size of temp is 0 means no element found then print 0

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.