Print nodes at distance k from a given node

sir/mam,
Please explain me the solution .i am not able to follow the video solution .

@Korou hey,The distance can be taken for node a higher, lower or at the same level. So, we will return nodes accordingly.

To solve this problem we have to understand what are the types of nodes that are K distance away from the target node.

From the above exam, we can see the k distant nodes can be in the subtree of the target node (5 and 1) or anywhere in the subtree of the ancestor of the target node(3).

Now, the method to find the solution of the first case is by traversing the subtree of the target node, and check if the distance of a node from the target is K. If yes print the node.

For the second case, we have to check the ancestor nodes and the subtree of these ancestors for the target and print all at distance K from it.

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.