Be the first user to complete this post

  • 0
Add to List

Print All The Nodes Which are X distance from the Root

Objective: - Given Binary Tree, Print all the nodes which are X distance from the root

Example :

Nodes at X distances from root
Nodes at X distances from root

Appraoch:

  • The idea is very simple.
  • Do the preorder traversal, pass x as a parameter.
  • While going down, at each level, keep reducing the x by 1
  • When x = 0, the means you have reached the nodes which are at x distance from the root, Print them.

Code:


Output:

Output:
Nodes at 3 distance from root :
 6 7 9



Also Read: