My code for this problem is https://ide.codingblocks.com/s/106434
it is not working for some cases please help me out in finding error.
Wrong answer for some test case
The implementation of your printatk() function is slightly wrong as you have to check for the ancestors of left and right child separately depending upon the location of target node.
Steps:
-
Base Case 1: If tree is empty, return -1
-
If a is same as root. Use the printklevel() function to print all nodes at distance k in subtree rooted with a or root and return 0.
-
Recur for left subtree.
-
Check if target node was found in left subtree
4.1.If root is at distance k from target, print root.
Note that left is Distance of root’s left child from target.
4.2.Else go to right subtree and print all b-left-2 distant nodes
Note that the right child is 2 edges away from left child.
4.3.Add 1 to the distance and return value for parent calls -
Similar implementation for right subtree. (step 3 and 4)
-
If target was neither present in left nor in right subtree
Hope, this would help.
Give a like, if you are satisfied.
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.