Algorithms to Check Sum of Two Numbers in Binary Search Trees


You are given two binary search trees a and b and an integer target. Return whether there’s a number in a and a number in b such that their sum equals to target

sum-of-two-numbers-in-two-bst-tree Algorithms to Check Sum of Two Numbers in Binary Search Trees algorithms c / c++ Depth First Search DFS Recursion recursive Two Pointer
Constraints
n ≤ 100,000 where n is the number of nodes in a
m ≤ 100,000 where m is the number of nodes in b

Recursive Algorithm to Find Sum of Two Numbers in BSTs

If the target is found by summing the two nodes in the current two trees A and B, great, return immediately. Otherwise, if the sum is bigger than the target, we can reduce the sum by either moving A to its left or B to its left. Similarly if the sum is smaller than the target, we can increase the sum by either moving A to its right or B to its right tree.

The complexity is O((logA*logB)^2). Space requirement is O(LogA+LogB).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
 * class Tree {
 *     public:
 *         int val;
 *         Tree *left;
 *         Tree *right;
 * };
 */
bool sumOfTwoNumberInBST(Tree* a, Tree* b, int target) {
    if (!a || !b) return false;
    if (a->val + b->val == target) return true;
    if (a->val + b->val < target) {
        return sumOfTwoNumberInBST(a->right, b, target) ||
                sumOfTwoNumberInBST(a, b->right, target);
    } else {
        return sumOfTwoNumberInBST(a->left, b, target) ||
                sumOfTwoNumberInBST(a, b->left, target);
    }
}
/**
 * class Tree {
 *     public:
 *         int val;
 *         Tree *left;
 *         Tree *right;
 * };
 */
bool sumOfTwoNumberInBST(Tree* a, Tree* b, int target) {
    if (!a || !b) return false;
    if (a->val + b->val == target) return true;
    if (a->val + b->val < target) {
        return sumOfTwoNumberInBST(a->right, b, target) ||
                sumOfTwoNumberInBST(a, b->right, target);
    } else {
        return sumOfTwoNumberInBST(a->left, b, target) ||
                sumOfTwoNumberInBST(a, b->left, target);
    }
}

Binary Search Algorithm to Find Sum in Two Binary Search Trees

Binary Search Trees are suitable for “Binary Search”. We can traverse one tree say A, and search for (target – A) in Tree B. The complexity is O(ALogB) or O(BLogA) assuming both Binary Search Trees are highly balanced. As we are recursively traversing two trees, the space complexity is O(LogA+LogB) due to implicitly space from Recursion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/**
 * class Tree {
 *     public:
 *         int val;
 *         Tree *left;
 *         Tree *right;
 * };
 */
bool sumOfTwoNumberInBST(Tree* a, Tree* b, int target) {
    function<bool(Tree*, int)> search = [](Tree* root, int target) {
        while (root) {
            if (target < root->val) {
                root = root->left;
            } else if (target > root->val) {
                root = root->right;
            } else {
                return true;
            }
        }
        return false;
    };
    function<bool(Tree*)> dfs = [&](Tree* root) {
        if (!root) return false;
        if (search(b, target - root->val)) return true;
        if (dfs(root->left)) return true;
        if (dfs(root->right)) return true;
        return false;
    };
    return dfs(a);
}
/**
 * class Tree {
 *     public:
 *         int val;
 *         Tree *left;
 *         Tree *right;
 * };
 */
bool sumOfTwoNumberInBST(Tree* a, Tree* b, int target) {
    function<bool(Tree*, int)> search = [](Tree* root, int target) {
        while (root) {
            if (target < root->val) {
                root = root->left;
            } else if (target > root->val) {
                root = root->right;
            } else {
                return true;
            }
        }
        return false;
    };
    function<bool(Tree*)> dfs = [&](Tree* root) {
        if (!root) return false;
        if (search(b, target - root->val)) return true;
        if (dfs(root->left)) return true;
        if (dfs(root->right)) return true;
        return false;
    };
    return dfs(a);
}

See same algorithms that are implemented in Python: Teaching Kids Programming – Recursive Algorithm to Find the Sum of Two Numbers in BSTs

Two Sum Variation Problems

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
555 words
Last Post: Dynamic Programming Algorithm to Solve 0-1 Knapsack Problem
Next Post: Simple Vigenère Cipher in C++

The Permanent URL is: Algorithms to Check Sum of Two Numbers in Binary Search Trees

Leave a Reply