i have approached this problem using dfs and stacks but i am not getting correct op . so how to this problem ? any hint
Take a generic tree input where you are first given the data of the node and then its no of children. The input is of preorder form and it is assured that the no of children will not exceed 2. The input of the tree is followed by a single integer K
hello @Ayush_Gupta
just run a dfs and maintain level of node.
if level of node is equal to k then include it in ur answer.
why r u using stack , when u r already connecting parent with its children using returned value of recursion
refer ths->

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.