![]() |
VOOZH | about |
Given a binary tree in which nodes are numbered from 1 to n. Given a node and a positive integer K. We have to print the Kth ancestor of the given node in the binary tree. If there does not exist any such ancestor then print -1.
For example in the below given binary tree, the 2nd ancestor of 5 is 1. 3rd ancestor of node 5 will be -1.
We have discussed a BFS-based solution for this problem in our previous article. If you observe that solution carefully, you will see that the basic approach was to first find the node and then backtrack to the kth parent. The same thing can be done using recursive DFS without using an extra array.
The idea of using DFS is to first find the given node in the tree and then backtrack k times to reach the kth ancestor. Once we have reached the kth parent, we will simply print the node and return NULL.
Below is the implementation of the above idea:
Kth ancestor is: 1
Time Complexity: O(n), where n is the number of nodes in the binary tree.
Auxiliary Space: O(h) where h is the height of binary tree due to recursion call.