Leetcode Problem 1214. Two Sum BSTs

1214. Two Sum BSTs

Leetcode Solutions

Approach: Binary Search

  1. Define a function existsInBST that takes a root of a BST and a target value, and performs a binary search to find the target value in the BST.
  2. Traverse the first tree using DFS.
  3. For each node in the first tree, call existsInBST with the root of the second tree and target - value1.
  4. If existsInBST returns true, it means a pair has been found, so return true.
  5. If no pair is found after traversing the entire first tree, return false.
UML Thumbnail

Approach: Brute Force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...