plz the the error in my code test cases are failing
đź’ˇ Print all nodes at distance k from a given node
I think you should pass “root” instead of “node1” to function “distancefromroot” in line 181
In line 145 you should pass d as parameter instead of q. But this would also not make your code work. I think your logic is wrong. What is the logic that you are trying to use to solve this problem ?
For what purpose are you calling the function distancefromroot in line 181 with parameters “node1” and “d”
just to get the root on the other side of the node as the question says i have to print the nodes present at a distance oh k so to print the nodes which are on the upper sied or we can say the other side i firstly find the distance of given element from root than after that i did k-p which i used to print the root of the other side
if u find my logic wrong than can u explain me the correct logic
So as you want to find the nodes at a distance k on the other side (or we can say on the upper side) of that node, you calculated an int i = k - p. Do you think that nodes(at a distance of k from given node) on that other side will always be at a distance of i = k - p from the root
Here in this image lets suppose we need to find nodes at a distance of k = 4 from node 9
Then what you can do is
find nodes at a distance of 1 on the right side of node 1
find nodes at a distance of 2 on the right side of node 2
find nodes at a distance of 3 on the left side of node 4
find nodes at a distance of 4 below node 9
I hope you understood the logic, if Not you can reply to this thread.
but how i am supposed to do this unable to crack the algo can u help me with the algo
Hello @ayush1213
Here is my code https://ide.codingblocks.com/s/157666
You should dry run this code for d = 9 and k = 4 to see how it works
What you will see is that the code first looks for the node 9
when it finds node 9, it has 1->2->4->9 as a path to node 9 in its recursion stack
After finding 9 it starts to backtrack
First it prints the nodes at a distance of 4 below node 9 using “PrintNodesBelowThisNodeFunction”
Then it backtracks to its parent node 4 and prints nodes at a distance of 3 in the left subtree of the node 4
Then it backtracks to its parent node 2 and prints nodes at a distance of 2 in the right subtree of the node 2
Then it backtracks to its parent node 1 and prints nodes at a distance of 1 in the right subtree of the node 1
In the code, I have used -1 so that I can know that I have found the initial node and now I don’t have the need to search the tree anymore, just backtrack 9 then 4 then 2 then 1
i m unable to figureout the problem in my code
my approach is first from singtle chars to full string plz tell where am i wrong with the explanation why i am wrong
Hello @ayush1213
Please create a seperate doubt for this question as this is a completely different one.
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.