0
0
DSA Javascriptprogramming~20 mins

Two Sum in BST in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Two Sum in BST Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Two Sum in BST with target 9
What is the output of the following code that checks if there exist two nodes in the BST whose values sum to 9?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function findTarget(root, k) {
  const set = new Set();
  function dfs(node) {
    if (!node) return false;
    if (set.has(k - node.val)) return true;
    set.add(node.val);
    return dfs(node.left) || dfs(node.right);
  }
  return dfs(root);
}

const root = new TreeNode(5,
  new TreeNode(3,
    new TreeNode(2),
    new TreeNode(4)
  ),
  new TreeNode(6,
    null,
    new TreeNode(7)
  )
);

console.log(findTarget(root, 9));
ATypeError
Bfalse
Cundefined
Dtrue
Attempts:
2 left
💡 Hint
Think about whether any two nodes add up to 9 in the BST.
Predict Output
intermediate
2:00remaining
Output of Two Sum in BST with target 28
What is the output of the following code that checks if there exist two nodes in the BST whose values sum to 28?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function findTarget(root, k) {
  const set = new Set();
  function dfs(node) {
    if (!node) return false;
    if (set.has(k - node.val)) return true;
    set.add(node.val);
    return dfs(node.left) || dfs(node.right);
  }
  return dfs(root);
}

const root = new TreeNode(10,
  new TreeNode(5,
    new TreeNode(3),
    new TreeNode(7)
  ),
  new TreeNode(15,
    null,
    new TreeNode(18)
  )
);

console.log(findTarget(root, 28));
Atrue
Bfalse
Cnull
DReferenceError
Attempts:
2 left
💡 Hint
Check if any two nodes add up to 28 in the BST.
🔧 Debug
advanced
2:00remaining
Identify the error in Two Sum in BST implementation
What error will this code produce when run?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function findTarget(root, k) {
  const set = new Set();
  function dfs(node) {
    if (!node) return false;
    if (set.has(k - node.val)) return true;
    set.add(node.val);
    return dfs(node.left) || dfs(node.right);
  }
  return dfs(root);
}

const root = new TreeNode(1,
  new TreeNode(0),
  new TreeNode(3)
);

console.log(findTarget(root, "3"));
ANo error, outputs true
Btrue
Cfalse
DTypeError
Attempts:
2 left
💡 Hint
Consider JavaScript type coercion in arithmetic operations.
🧠 Conceptual
advanced
1:30remaining
Why use a Set in Two Sum in BST?
Why does the Two Sum in BST algorithm use a Set to store node values during traversal?
ATo count the frequency of each node value
BTo sort the node values for binary search
CTo keep track of visited nodes and check complements efficiently
DTo store nodes in insertion order for traversal
Attempts:
2 left
💡 Hint
Think about how to quickly check if a complement value exists.
🚀 Application
expert
2:30remaining
Number of pairs summing to target in BST
Given the BST below, how many unique pairs of nodes sum to 10? BST structure: 5 / \ 3 7 / \ \ 2 4 8
A1
B2
C3
D4
Attempts:
2 left
💡 Hint
List all pairs and count those summing to 10.