The lowest common ancestor of two nodes p and q is the lowest node in the binary search tree that has both p and q as its descendants. A node is also considered a descendant of itself.
Given the root node and two nodes p and q in a binary search tree, return their lowest common ancestor.
Note: You can assume that p and q will be present in the tree.
The first line contains an integer T denoting the number of test cases.
For each test case, the input has 3 lines:
For each test case, the output contains a line with the value of the lowest common ancestor for p and q.
4
9
2 1 3 -1 -1 -1 5 4 7
6 7
7
6 3 21 -1 -1 -1 89
0 6
12
8 3 9 -1 4 -1 10 -1 -1 -1 12 11
1 6
4
28 14 -1 11
0 3
5
6
8
28
1 <= T <= 10
1 <= n <= 105
1 <= node value <= 109
All nodes have unique values.