πŸš€Day 12. Pair Sum in BST 🧠

The problem can be found at the following link: Question Link

πŸ’‘ Problem Description:

Given a Binary Search Tree (BST) and a target value, check whether there exists a pair of nodes in the BST whose sum equals the given target.

πŸ” Example Walkthrough:

Example 1:

Input:

        7
       / \
      3   8
     / \    \
    2   4    9

Target = 12

Output:

True

Explanation:

In the given BST, there exist two nodes (8 and 4) that sum up to 12.

Example 2:

Input:

Target = 23

Output:

Explanation:

No pair of nodes in the BST sum up to 23.

Constraints:

  • $(1 \leq \text{Number of Nodes} \leq 10^5)$

  • $(1 \leq \text{target} \leq 10^6)$

🎯 My Approach:

Optimized Two-Pointer on Inorder Traversal (O(N) Time, O(N) Space)

  1. Convert BST to sorted array using inorder traversal.

  2. Use two-pointer approach (left and right) to find the target sum efficiently.

Algorithm Steps:

  1. Perform an inorder traversal and store the BST elements in a sorted list.

  2. Use two pointers (left at the start, right at the end) and check the sum:

    • If arr[left] + arr[right] == target, return true.

    • If arr[left] + arr[right] < target, move left++.

    • If arr[left] + arr[right] > target, move right--.

  3. If no such pair exists, return false.

πŸ•’ Time and Auxiliary Space Complexity

  • Expected Time Complexity: O(N), since we traverse each node once.

  • Expected Auxiliary Space Complexity: O(N), due to storing the inorder traversal.

πŸ“ Solution Code

Code (C++)

🌲 Alternative Approaches

2️⃣ Optimized Hash Set Approach (O(N) Time, O(H) Space)

  1. Use DFS to traverse the tree while maintaining a hash set of visited values.

  2. Check if (target - current node value) exists in the set, if yes, return true.

  3. Insert the current node value into the set and continue.

πŸ”Ή Avoids extra space for storing inorder traversal πŸ”Ή Uses Hash Set for O(1) lookup

3️⃣ BST Iterator + Two-Pointer (O(N) Time, O(H) Space)

  1. Use two iterators: one for inorder (left to right) and one for reverse inorder (right to left).

  2. Move iterators towards each other until the pair is found or iterators cross.

πŸ”Ή Optimized space using O(H) stack instead of O(N) vector πŸ”Ή Uses BST Iterators for an efficient two-pointer search

Comparison of Approaches

Approach

⏱️ Time Complexity

πŸ—‚οΈ Space Complexity

⚑ Method

βœ… Pros

⚠️ Cons

Two-Pointer on Inorder Array

🟒 O(N)

🟑 O(N)

Two Pointers

Simple and efficient

Extra space for list

Hash Set (DFS)

🟒 O(N)

🟑 O(N)

Hashing

Faster lookup, no sorting needed

Hashing overhead

BST Iterator (Two-Pointer)

🟑 O(N^2)

🟒 O(H)

Recursion

No extra storage used

Slow for large BSTs


πŸ’‘ Best Choice?

  • For simplicity: βœ… Two-Pointer on Inorder Array is easiest to understand.

  • For faster lookups: βœ… Hash Set (O(H) space) avoids sorting overhead.

  • For minimal space: βœ… BST Iterator (O(H) space, O(N) time) avoids extra space but is slow for large BSTs.

Code (Java)

Code (Python)

🎯 Contribution and Support:

For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!

⭐ If you find this helpful, please give this repository a star! ⭐


πŸ“Visitor Count

Last updated