Program to find all the nodes at k distance from the root in a binary tree is discussed here. Given a binary tree, print all the nodes which are at k distance from the root.
For example, consider the tree given below.
15n / n 12 18 n / /n 8 13 24n / \n 22 27n
Solution to this problem can be provided in two different ways.
Method 1: Iterative approach to find all the nodes at k distance from the root.
Method 2: Recursive approach to find all the nodes at k distance from the root.
Iterative program to find all the nodes at K distance from the root is given below.
@@coding::1@@
print_nodes_k_Distance( node *root, k )
Recursive program to find all the nodes at K distance from the root is given below.
@@coding::2@@
Time complexity: O(n)
Recommended Programs