Bird
Raised Fist0
DSA Typescriptprogramming~5 mins

Two Sum in BST in DSA Typescript - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the main goal of the Two Sum problem in a Binary Search Tree (BST)?
To find if there exist two nodes in the BST whose values add up to a given target sum.
Click to reveal answer
intermediate
Why is BST property useful in solving the Two Sum problem?
Because BST nodes are ordered, we can efficiently search for complements using in-order traversal or two-pointer techniques.
Click to reveal answer
beginner
What traversal method is commonly used to get sorted node values from a BST?
In-order traversal, which visits nodes in ascending order of their values.
Click to reveal answer
intermediate
Explain the two-pointer technique in the context of Two Sum in BST.
After getting sorted values from BST, use two pointers at start and end to check sums. Move pointers inward based on sum comparison to target.
Click to reveal answer
intermediate
What is the time complexity of the Two Sum in BST solution using in-order traversal and two pointers?
O(n), where n is the number of nodes, because we traverse all nodes once and then use two pointers on the sorted list.
Click to reveal answer
Which traversal gives sorted node values in a BST?
AIn-order traversal
BPre-order traversal
CPost-order traversal
DLevel-order traversal
What does the two-pointer technique do in Two Sum in BST?
AChecks pairs from start and end of sorted values to find target sum
BSearches each node's children recursively
CBuilds a hash map of node values
DDeletes nodes that don't match the sum
What is the space complexity of storing BST values in a list for Two Sum?
AO(1)
BO(log n)
CO(n)
DO(n log n)
If BST has 7 nodes, what is the maximum number of pairs checked by two-pointer method?
A7
BLess than 7
C3
D6
Which data structure can be used as an alternative to two-pointer for Two Sum in BST?
ALinked list
BQueue
CStack
DHash set
Describe the step-by-step approach to solve Two Sum in BST using in-order traversal and two pointers.
Think about how sorted order helps find pairs efficiently.
You got /5 concepts.
    Explain why the BST property helps reduce the search space in the Two Sum problem compared to an unsorted tree.
    Focus on ordering and how it simplifies searching.
    You got /4 concepts.

      Practice

      (1/5)
      1. What is the main goal of the Two Sum in BST problem?
      easy
      A. Check if a BST is balanced
      B. Find two nodes in a BST whose values add up to a target sum
      C. Count the number of nodes in a BST
      D. Find the maximum value in a BST

      Solution

      1. Step 1: Understand the problem statement

        The problem asks to find two nodes in a Binary Search Tree (BST) such that their values add up to a given target sum.
      2. Step 2: Identify the goal

        The goal is to check if such a pair exists, not to find maximum, count nodes, or check balance.
      3. Final Answer:

        Find two nodes in a BST whose values add up to a target sum -> Option B
      4. Quick Check:

        Two Sum in BST = Find pair with target sum [OK]
      Hint: Look for two values adding to target in BST [OK]
      Common Mistakes:
      • Confusing with finding max or min value
      • Thinking it counts nodes instead of finding pairs
      • Assuming it checks tree balance
      2. Which TypeScript syntax correctly defines a BST node for the Two Sum problem?
      easy
      A. interface TreeNode { val: string; left: TreeNode; right: TreeNode; }
      B. function TreeNode(val) { this.val = val; this.left = null; this.right = null; }
      C. type TreeNode = { val: number; left?: TreeNode; right?: TreeNode; }
      D. class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } }

      Solution

      1. Step 1: Check correct TypeScript class syntax for BST node

        class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } defines a class with number value and nullable left/right children, matching BST node structure.
      2. Step 2: Compare other options

        function TreeNode(val) { this.val = val; this.left = null; this.right = null; } is JavaScript function style, not TypeScript class. interface TreeNode { val: string; left: TreeNode; right: TreeNode; } uses string type for val and non-nullable children, which is incorrect. type TreeNode = { val: number; left?: TreeNode; right?: TreeNode; } uses optional properties but lacks constructor and proper null handling.
      3. Final Answer:

        class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } -> Option D
      4. Quick Check:

        Correct BST node class syntax = class TreeNode { val: number; left: TreeNode | null; right: TreeNode | null; constructor(val: number) { this.val = val; this.left = null; this.right = null; } } [OK]
      Hint: Use class with nullable left/right and number val [OK]
      Common Mistakes:
      • Using string type for node value
      • Not initializing left and right to null
      • Using JavaScript function instead of TypeScript class
      3. Given the BST below and target = 9, what will the Two Sum function return?
          5
         / \
        3   7
       /     \
      2       8
      
      Function uses in-order traversal and a set to find pairs.
      medium
      A. true
      B. false
      C. null
      D. Error

      Solution

      1. Step 1: Traverse BST in-order and check pairs

        In-order traversal gives sorted values: [2, 3, 5, 7, 8]. We check if target - current value exists in set.
      2. Step 2: Find pair summing to 9

        Check pairs: 2 + 7 = 9 (found), so function returns true.
      3. Final Answer:

        true -> Option A
      4. Quick Check:

        2 + 7 = 9 found in BST [OK]
      Hint: In-order traversal gives sorted list to find pairs [OK]
      Common Mistakes:
      • Assuming no pair without checking all nodes
      • Confusing traversal order
      • Ignoring set lookup for complements
      4. Identify the bug in this TypeScript Two Sum in BST function snippet:
      function findTarget(root: TreeNode | null, k: number): boolean {
        const set = new Set<number>();
        function dfs(node: TreeNode | null): boolean {
          if (!node) return false;
          if (set.has(k - node.val)) return true;
          set.add(node.val);
          dfs(node.left);
          dfs(node.right);
          return false;
        }
        return dfs(root);
      }
      medium
      A. The base case should return true when node is null
      B. Set is not initialized properly
      C. The function returns false too early, missing true from recursive calls
      D. The function should use a queue instead of recursion

      Solution

      1. Step 1: Analyze recursive calls and return values

        The dfs function calls dfs(node.left) and dfs(node.right) but ignores their return values, always returning false at the end.
      2. Step 2: Identify missing return propagation

        If dfs(node.left) or dfs(node.right) returns true, dfs should return true immediately to propagate the success.
      3. Final Answer:

        The function returns false too early, missing true from recursive calls -> Option C
      4. Quick Check:

        Return recursive true results to avoid missing pairs [OK]
      Hint: Return recursive calls' results to propagate true [OK]
      Common Mistakes:
      • Ignoring return values of recursive calls
      • Returning false before checking all nodes
      • Misunderstanding base case return
      5. You want to optimize Two Sum in BST for large trees. Which approach best reduces time complexity while using O(h) space, where h is tree height?
      hard
      A. Use two pointers with iterative in-order and reverse in-order traversals simultaneously
      B. Convert BST to array and use nested loops to find pairs
      C. Use breadth-first search with a hash set to find pairs
      D. Perform recursive DFS and store all node values in a list before checking pairs

      Solution

      1. Step 1: Understand space and time constraints

        We want to reduce time complexity and keep space to O(h), where h is tree height.
      2. Step 2: Evaluate approaches

        Use two pointers with iterative in-order and reverse in-order traversals simultaneously uses two pointers with iterative in-order (smallest to largest) and reverse in-order (largest to smallest) traversals, allowing pair search in O(n) time and O(h) space. Other options use extra space O(n) or nested loops with O(n^2) time.
      3. Final Answer:

        Use two pointers with iterative in-order and reverse in-order traversals simultaneously -> Option A
      4. Quick Check:

        Two pointer BST traversal = O(n) time, O(h) space [OK]
      Hint: Use two pointers with in-order and reverse in-order iterators [OK]
      Common Mistakes:
      • Using nested loops causing O(n^2) time
      • Storing all nodes in list increasing space to O(n)
      • Using BFS which uses more space than O(h)
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard