Given a Binary Search Tree and two node values x and y present in the BST. Your task is to find the pth (p >= 1) common ancestor of these two nodes x and y.

The 1st common ancestor is the lowest common ancestor. The lowest common ancestor between two nodes n1 and n2 is defined as the lowest node in T that has both n1 and n2 as descendants (where we allow a node to be a descendant of itself).

All the elements of BST are non-negative and there is no duplicate entry in BST. Also, x and y are unequal.

You are required to complete the function **pthCommonAncestor****()**.

Return -1 if the pth common ancestor doesn’t exist for a given BST.

**Note:** The **Input/****Ouput** format and **Example** given are used for system's internal purpose, and should be used by a user for **Expected Output** only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

**Input:**

The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consists of three lines. The first line of each test case consists of two integers N and p, denoting the number of elements in the BST and the p^{th} common ancestor to be found, respectively. The second line of each test case consists of N space-separated integers denoting the elements in the BST. The third line of each test case consists of two integers x and y as described in the problem statement.

**Output:**

You are required to complete the function **pthCommonAncestor() **which takes the root of the tree and two integers x and y(as described in the problem statement) as the arguments. The function returns the value of p^{th} common ancestor to be found.

**Constraints:**

1 <= T <= 1000

2 <= N <= 1000

1 <= p <= 100

**Example:**

**Input:**

2

6 2

50 30 70 60 55 65

55 65

5 2

6 3 8 1 4

1 4

**Output:**

70

6

**Explanation:**

The BST for the **sample test case 1** is-

50 // 50 is the 3rd common ancestor of 55 and 65

/ \

30 70 // 70 is the 2nd common ancestor of 55 and 65

/

60 // 60 is the 1st common ancestor of 55 and 65

/ \

55 65

Hence, the value of 2nd common ancestor of 55 and 65 is 70.

The BST for the **sample test case 2** is-

6 // 6 is the 2nd common ancestor of 1 and 4

/ \

3 8 // 3 is the 1st common ancestor of 1 and 4

/ \

1 4

Hence, the value of 2nd common ancestor of 1 and 4 is 6.

Author: sajalagrawal

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

ShubhamJain18 | 205 |

makeitdonesolo | 140 |

lost_found | 124 |

maggiiiii | 118 |

SahatPrasad1 | 108 |

Bishnu Dev Panda | 777 |

okayboss | 551 |

ShubhamJain18 | 516 |

yk12 | 514 |

makeitdonesolo | 480 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4925 |

Quandray | 4547 |

Login to report an issue on this page.