PEC-CSS/Open-Source

Kth ancestor of a node in binary tree

Gauravsharma-20 opened this issue · 0 comments

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 above given binary tree, the 2nd ancestor of 5 is 1. 3rd ancestor of node 5 will be -1.

struct Node
{
    int data;
    struct Node *left, *right;
};