Ancestors in Binary Tree
Given a Binary Tree and a target key, write a function that prints all the ancestors of the key in the given binary tree.
Let's the tree be like following:
Let for node value 12: Ancestors are: 7, 5, 8 While for node value 7: Ancestors are: 5, 8
What is Ancestors?
For any node n,
Its ancestors are the nodes which are on the path between roots to node n
Thus for the above examples,
That simply means we are doing kind of DFS
For a currentnode to be ancestor of the target node the conditions are:
Example with explanation: