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.
- Given a binary tree, print out all of its root-to-leaf paths one per line.
- Check sum of Covered and Uncovered nodes of Binary Tree
- Program to count leaf nodes in a binary tree
- Root to leaf path sum equal to a given number
- Given a binary tree, print all root-to-leaf paths
- Print nodes at k distance from root
- Connect nodes at same level
- Print BST keys in the given range
- Print Ancestors of a given node in Binary Tree
- Connect nodes at same level using constant extra space
- Populate Inorder Successor for all nodes
- Find the maximum sum leaf to root path in a Binary Tree
- Check whether a given Binary Tree is Complete or not | Set 1 (Iterative Solution)
- Iterative Preorder Traversal
- Iterative Postorder Traversal | Set 1 (Using Two Stacks)