Two Sum in Binary Search Tree (BST) šŸŽÆ

beginner
15 min

Two Sum in Binary Search Tree (BST) šŸŽÆ

Welcome to this comprehensive guide on finding the two sum problem in a Binary Search Tree (BST)! We'll walk through the problem, understand why it's important, and learn how to solve it with practical examples. Let's dive in!

Introduction šŸ“

In this lesson, we'll learn to find a pair of nodes in a Binary Search Tree (BST) that add up to a given sum. This problem is a common question during technical interviews and is an essential concept in data structures and algorithms.

Prerequisites šŸ’”

Before diving into the Two Sum problem in BST, make sure you're familiar with the following topics:

Problem Statement šŸ“

Given a Binary Search Tree (BST) and an integer targetSum, find a pair of nodes in the tree whose values add up to the targetSum.

Solution Approach šŸ’”

To solve the Two Sum problem in a BST, we can use the Inorder Traversal property of BSTs and recursion.

  1. Traverse the BST in Inorder (Left -> Root -> Right)
  2. Maintain a variable currentSum and compare each node's value with targetSum - currentSum. If they are equal, we've found the desired pair.
  3. If currentSum + node.value is less than targetSum, update currentSum and move to the right subtree.
  4. If currentSum + node.value is greater than targetSum, move to the left subtree.
  5. Repeat the process until the entire BST is traversed or we find the desired pair.

Example šŸ’”

Let's consider the following BST:

5 / \ 3 7 /\ /\ 2 4 6 8

To find a pair that adds up to 9, we'll traverse the tree as follows:

  1. Start from the root (5). Since targetSum - 5 (i.e., 4) is greater than 2 (3, the left child of 5), move to the left subtree.
  2. Traverse the left subtree and find 3. Since targetSum - 3 (i.e., 6) is less than 5 (the right child of 3), move to the right subtree.
  3. Find 2 in the right subtree of 3. Since targetSum - 2 (i.e., 7) is less than 7 (the right child of 3), move to the right subtree.
  4. Find 4 in the right subtree of 7. Since targetSum - 4 (i.e., 5) equals the current node's value (4), we've found the desired pair (4, 5).

Code Example šŸ’”

Here's a Python implementation of the Two Sum problem in a BST:

python
class Node: def __init__(self, key): self.left = None self.right = None self.val = key def findPairWithGivenSum(root, targetSum): def helper(node, currentSum): if not node: return False if currentSum < targetSum: return helper(node.right, currentSum + node.val) if currentSum > targetSum: return helper(node.left, currentSum + node.val) return True return helper(root, 0) # Test the function root = Node(5) root.left = Node(3) root.right = Node(7) root.left.left = Node(2) root.left.right = Node(4) root.right.left = Node(6) root.right.right = Node(8) print(findPairWithGivenSum(root, 9)) # True (Found: (4, 5))

Quiz šŸ’”

Quick Quiz
Question 1 of 1

In the Two Sum problem in a BST, what is the purpose of maintaining a `currentSum` variable?

That's it for today! By learning how to solve the Two Sum problem in a Binary Search Tree, you've taken a significant step towards mastering important data structures and algorithms concepts. Keep practicing and happy coding! šŸ¤–āœØ