I AM NOT ABLE TO RUN MY CODE BECAUSE OF HOW TO TAKE TARGET NODE AS INPUT CAN YOU PLEASE WRITE THE CODE FOR TAKING TARGET NODE AS INPUT AND RUN IT AND SEND ME
THIS IS MY CODE: https://ide.codingblocks.com/s/244762
I AM NOT ABLE TO RUN MY CODE BECAUSE OF HOW TO TAKE TARGET NODE AS INPUT CAN YOU PLEASE WRITE THE CODE FOR TAKING TARGET NODE AS INPUT AND RUN IT AND SEND ME
THIS IS MY CODE: https://ide.codingblocks.com/s/244762
@navin_japes
You can simply take target node as int input Then do it accordingly.
Also declare map mp at global level, after the TreeNode class. otherwise it may be giving some error. Also somewhere you used data, somewhere val.
No I have to take the reference of target node not integer input so how to do it and I have corrected that val and data please correct the code according to you to give nodes at a distance k from target node.
Please reply fast brother.
@navin_japes
As you know the value of target node, so you can just do a preorder traversal or any traversal and find the reference to target node.
code will be like:-
Node *find(Node *root, int target) {
if(root==NULL) return NULL;
if(root==target) return root;
Node l = find(root->left, target);
if(l!=NULL) return l;
return find(root->right, target);
}
No I have to take the input from the user target node and K and then I need to find out the nodes at a distance k from target node
Modify in my code if the target node and K given as input find all the nodes at a distance k from target node
@navin_japes
I have explained it to you. Also provided the function. Now you can easily do the changes in your code accordingly. Just try to do it
No you haven’t explained it
I want to take target node as input from the user and that node is not pre-known to me
Don’t try to skip it I told you to modify in my code how to take target node as input and you are not doing that ,
Telling is easy just try to modify and see it’s not giving correct output
@navin_japes
i am not trying to skip it, I was just trying to guide you to correct path because its nice if we can identify and correct our code with seeing the solution.
i explained the logic and told you to implement it because i wasn’t sure about the input format you are using (preorder input you want right?) and your approach is also different than mine.
If you want me correct then please give me some time, i almost got your approach but still give me little brief of your code and approach from your side for some confirmation.
Don’t worry i will be surely replying to you again in some time.
See ,I have used map to store the parent of each node so it is helpful while traversing back and doing breadth first search using queue,so first I have stored target node and for that node I store the parent ,left and the right child and increase the layer by 1
If you don’t understand any part you can ping me I will explain but please try to correct the code,as I am stuck from so long.
@navin_japes
here is your code https://ide.codingblocks.com/s/244883
I have done the required changes and tried to handle all the cases.
Let me know, now is it fine for you ?
Thanks you bro it was how I wanted thank you very much.
I will rate in the morning as power is not there, I am not able to rate it now.
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.