Save 25%Black Friday: 25% off all plans — use code BLACKFRIDAY2026 at checkout. Ends Friday, Dec 4.Claim the deal

Given the roots of two Binary Search Trees (BSTs) containing integer values, find the greatest common integer present in both trees. If there is no common integer, return -1.

Constraints:

  • The number of nodes in both trees is in the range [1,105][1, 10^5].
  • −109≤Node.val≤109-10^9 \leq \text{Node.val} \leq 10^9
  • Both trees are valid BSTs.
Test Cases
Input
Output
Expected