Given a Binary Tree and a target key, write a function that prints all the ancestors of the key in the given binary tree
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
total answers (1)
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,
Example 1:
Example 2:
Algorithm:
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:
C++ Implementation:
Output