0
0
DSA Javascriptprogramming~10 mins

Two Sum in BST in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Two Sum in BST
Start at root
Initialize two pointers
Left pointer = smallest node
Right pointer = largest node
Check sum = left.val + right.val
Return true
Move left pointer to next larger node
Move right pointer to next smaller node
Repeat until pointers meet
Return false if no pair found
We use two pointers starting at smallest and largest nodes in the BST, moving inward to find two nodes summing to target.
Execution Sample
DSA Javascript
function findTarget(root, k) {
  let left = getMinNode(root);
  let right = getMaxNode(root);
  while (left !== right) {
    let sum = left.val + right.val;
    if (sum === k) return true;
    else if (sum < k) left = getNextLargerNode(left);
    else right = getNextSmallerNode(right);
  }
  return false;
}
This code tries to find two nodes in a BST whose values add up to k using two pointers moving inward.
Execution Table
StepOperationLeft Pointer NodeRight Pointer NodeSumActionVisual State
1Initialize pointersNode(2)Node(9)2 + 9 = 1111 > 7, move right pointer left2 -> 3 -> 5 -> 7 -> 9
2Move right pointerNode(2)Node(7)2 + 7 = 99 > 7, move right pointer left2 -> 3 -> 5 -> 7 -> 9
3Move right pointerNode(2)Node(5)2 + 5 = 77 == 7, return true2 -> 3 -> 5 -> 7 -> 9
4Return---Found pair (2,5) equals target 72 -> 3 -> 5 -> 7 -> 9
💡 Pointers met or found pair summing to target; here pair (2,5) sums to 7, so return true.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
leftNode(2)Node(2)Node(2)Node(2)Node(2)
rightNode(9)Node(9)Node(7)Node(5)Node(5)
sum-11977
Key Moments - 3 Insights
Why do we move the right pointer left when sum is greater than target?
Because the BST is sorted, moving the right pointer to the next smaller node decreases the sum, as shown in steps 1 and 2 in the execution_table.
Why do we stop when left and right pointers meet?
When both pointers meet, it means all pairs have been checked without finding the target sum, so we stop. In this example, we found the pair before they met (step 3).
How do we find the next larger or smaller node in BST?
We use in-order traversal logic: next larger node is the in-order successor, next smaller node is the in-order predecessor. This is implied in pointer moves in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value of the right pointer node?
ANode(7)
BNode(5)
CNode(9)
DNode(2)
💡 Hint
Check the 'Right Pointer Node' column at step 2 in the execution_table.
At which step does the sum equal the target value 7?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Sum' and 'Action' columns in the execution_table to find when sum == target.
If the target was 10 instead of 7, what would happen at step 3?
AMove right pointer to next smaller node
BReturn true because sum equals target
CMove left pointer to next larger node
DStop immediately
💡 Hint
Refer to the 'Action' column logic in the execution_table for sum < target cases.
Concept Snapshot
Two Sum in BST:
- Use two pointers: left at smallest node, right at largest node
- Calculate sum = left.val + right.val
- If sum == target, return true
- If sum < target, move left pointer to next larger node
- If sum > target, move right pointer to next smaller node
- Stop when pointers meet or pair found
Full Transcript
The Two Sum in BST problem uses two pointers starting at the smallest and largest nodes of the BST. We calculate the sum of their values. If the sum equals the target, we return true. If the sum is less than the target, we move the left pointer to the next larger node to increase the sum. If the sum is greater than the target, we move the right pointer to the next smaller node to decrease the sum. We repeat this until the pointers meet or we find a pair that sums to the target. This approach leverages the BST's sorted property to efficiently find the pair without checking all nodes.