In this problem, we are given a binary tree and a key k. We need to find out all the ancestors of k.
For example, if k = 7 and the given tree is -

Then, the ancestors of 7 will be 4,2 and 1.
You can simply solve this problem using standard traversal techniques like:
Breadth First SearchDepth First SearchLevel order traversal
The idea is to do that traversal recursively and return 1 when you reach the given node K and return 0 for all other cases. What this does is that: All nodes coming bef...
Published on March 10, 2021 08:02