
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Two sum in BSTs in JavaScript
Problem:
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.
For example, if the input to the function is −
const target = 23;
BSTs
Then the output should be −
const output = true;
Output Explanation:
Because there exists 6 in the first tree and 17 in the second, whose sum is 23.
Example
The code for this will be −
class Node{ constructor(data) { this.data = data; this.left = null; this.right = null; }; }; class BinarySearchTree{ constructor(){ // root of a binary seach tree 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); }; }; }; }; const tree1 = new BinarySearchTree(); tree1.insert(1); tree1.insert(3); tree1.insert(4); tree1.insert(2); tree1.insert(4); tree1.insert(5); tree1.insert(6); 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
And the output in the console will be −
true
- Related Articles
- Two Sum BSTs in C++
- Count pairs from two BSTs whose sum is equal to a given value x in C++
- Find pairs with given sum such that pair elements lie in different BSTs in Python
- Two sum problem in linear time in JavaScript
- Reverse sum of two arrays in JavaScript
- Multiply and Sum Two Arrays in JavaScript
- Subarray sum with at least two elements in JavaScript
- Is a number sum of two perfect squares in JavaScript
- Sum of two elements just less than n in JavaScript\n
- Check for Identical BSTs without building the trees in C++
- Two Sum in Python
- JavaScript Sum of two objects with same properties
- Return the sum of two consecutive elements from the original array in JavaScript
- Finding two numbers that produce equal to the sum of rest in JavaScript
- Subarrays product sum in JavaScript

Advertisements