Two Sum in a BST Problem Statement

Given a Binary Search Tree (BST) and a target value, determine if there exists a pair of node values in the BST whose sum equals the target value.

Example:

Input:
4 2 6 1 3 5 7 -1 -1 -1 -1 -1 -1 -1 -1
9
Output:
True
Explanation:

Using the tree structure defined by the level order input, find two nodes such that their value sums up to the given target value, here 9.

Constraints:

  • 1 <= T <= 100
  • 1 <= N <= 3000
  • -10^9 <= node data <= 10^9 (node data is not -1)
  • -10^9 <= target value <= 10^9
Note:

You are expected to implement the function only. The input/output operations are handled for you.

Be the first one to answer
Add answer anonymously...
Morgan Stanley Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter