Two sum in BSTs in JavaScript

We are required to write a JavaScript function that takes in the roots of two binary search trees, root1 and root2, as the first and the second argument respectively. The third argument to the function is number, target.

Our function should return True if and only if there is a node in the first tree and a node in the second tree whose values sum up to a given integer target, false otherwise.

Problem Example

For example, if the input to the function is:

const target = 23;
BST 1 4 2 5 1 3 6 BST 2 9 6 11 3 8 10 17 6 + 17 = 23 ?

Then the output should be:

const output = true;

Because there exists 6 in the first tree and 17 in the second, whose sum is 23.

Solution Approach

The algorithm uses a recursive approach that leverages the BST property. Since both trees are sorted, we can navigate efficiently:

  • If current sum equals target, return true
  • If current sum is less than target, move to larger values (right nodes)
  • If current sum is greater than target, move to smaller values (left nodes)

Implementation

class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}

class BinarySearchTree {
    constructor() {
        this.root = null;
    }
    
    insert(data) {
        var newNode = new Node(data);
        if (this.root === null) {
            this.root = newNode;
        } else {
            this.insertNode(this.root, newNode);
        }
    }
    
    insertNode(node, newNode) {
        if (newNode.data < node.data) {
            if (node.left === null) {
                node.left = newNode;
            } else {
                this.insertNode(node.left, newNode);
            }
        } else {
            if (node.right === null) {
                node.right = newNode;
            } else {
                this.insertNode(node.right, newNode);
            }
        }
    }
}

// Create first BST
const tree1 = new BinarySearchTree();
tree1.insert(4);
tree1.insert(2);
tree1.insert(5);
tree1.insert(1);
tree1.insert(3);
tree1.insert(6);

// Create second BST
const tree2 = new BinarySearchTree();
tree2.insert(9);
tree2.insert(6);
tree2.insert(11);
tree2.insert(3);
tree2.insert(8);
tree2.insert(10);
tree2.insert(17);

const twoSumBSTs = (root1, root2, target) => {
    if (root1 == null || root2 == null) return false;
    
    if (root1.data + root2.data == target) return true;
    
    if (root1.data + root2.data < target) {
        return twoSumBSTs(root1.right, root2, target) || 
               twoSumBSTs(root1, root2.right, target);
    } else {
        return twoSumBSTs(root1.left, root2, target) || 
               twoSumBSTs(root1, root2.left, target);
    }
};

const target = 23;
console.log(twoSumBSTs(tree1.root, tree2.root, target));

Output

true

How It Works

The algorithm starts from both roots and compares their sum with the target:

  1. If sum equals target, we found the pair
  2. If sum is less than target, we need larger values, so move right in either tree
  3. If sum is greater than target, we need smaller values, so move left in either tree
  4. Continue recursively until we find a match or exhaust all possibilities

Time Complexity

Time complexity: O(m × n) where m and n are the number of nodes in each tree. Space complexity: O(h1 + h2) where h1 and h2 are the heights of the trees due to recursion stack.

Conclusion

This solution efficiently finds if two values from different BSTs sum to a target by leveraging the sorted property of BSTs. The recursive approach explores all valid combinations while pruning unnecessary paths.

Updated on: 2026-03-15T23:19:00+05:30

246 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements