Given a root of a tree, and an integer k. Print all the nodes which are at k distance from root.
Input : 20 / \ 10 30 / \ / \ 5 15 25 40 / 12 and k = 3 Root is at level 1. Output : 5 15 25 40
data at level 1 : 20 data at level 2 : 10 30 data at level 3 : 5 15 25 40 data at level 6 : Number exceeds total number of levels
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.