Finding Kth Ancestor Of A Node In Binary Tree is an example of null problems. In this post we will see how we can solve in Javascript.

Problem Description

K-th ancestor of a node in Binary Tree

  • 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 K-th 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, 2nd ancestor of node 4 and 5 is 1. 3rd ancestor of node 4 will be -1.

Solution: Please check the main.js snippet for the solution. If you have different approach in mind or have any suggestion for this implementation feel free to share in the comment below. Thanks!