Advertisement

Two Sum IV - Input is a BST - LeetCode 653 Solution

Two Sum IV - Input is a BST - Complete Solution Guide

Two Sum IV - Input is a BST is LeetCode problem 653, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the root of a binary search tree and an integer k , return true if there exist two elements in the BST such that their sum is equal to k , or false otherwise . Example 1: Input: root = [5,3,6,2,4,null,7], k = 9 Output: true Example 2: Input: root = [5,3,6,2,4,null,7], k = 28 Output: false Constraints: The number of nodes in the tree is in the range [1, 10 4 ] . -10 4 <= Node.val <= 10 4 root is guaranteed to be a valid binary search tree. -10 5 <= k <= 10 5

Detailed Explanation

The problem asks us to determine if there exist two distinct nodes within a given Binary Search Tree (BST) whose values sum up to a target integer 'k'. The input is the root node of the BST and the target value 'k'. The output should be a boolean: 'true' if such a pair exists, and 'false' otherwise. The constraints specify the size of the tree, the range of node values, and the range of the target value.

Solution Approach

The provided solutions use a Depth-First Search (DFS) traversal of the BST in combination with a hash set. The DFS explores each node, and for each node, it checks if the complement (k - node.val) already exists in the hash set. If it does, we've found a pair that sums to k, and we return 'true'. Otherwise, we add the current node's value to the hash set and continue the DFS traversal in both the left and right subtrees.

Step-by-Step Algorithm

  1. Step 1: Initialize a hash set (e.g., 'nums' in Python, 'seen' in Java and C++) to store the values of visited nodes.
  2. Step 2: Define a recursive function 'dfs(node)' that takes a node as input.
  3. Step 3: Inside 'dfs(node)', check if the node is null. If so, return 'false' because we've reached the end of a branch without finding a suitable pair.
  4. Step 4: Check if 'k - node.val' is present in the hash set. If it is, return 'true' because we've found a pair that sums to k.
  5. Step 5: Add 'node.val' to the hash set.
  6. Step 6: Recursively call 'dfs(node.left)' and 'dfs(node.right)' and return 'true' if either of these calls returns 'true'. This is because we only need to find one pair to satisfy the condition. If both return false then return false.
  7. Step 7: Call 'dfs(root)' to start the traversal from the root node and return the result.

Key Insights

  • Insight 1: The problem leverages the properties of a BST: values in the left subtree are smaller than the node's value, and values in the right subtree are larger.
  • Insight 2: Using a hash set (or set) allows for efficient O(1) average time complexity lookups to check if the complement (k - node.val) exists in the tree.
  • Insight 3: The problem can also be solved using an in-order traversal to store the BST elements into a sorted array, followed by a two-pointer approach for finding the sum.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Hash Table, Two Pointers, Tree, Depth-First Search, Breadth-First Search, Binary Search Tree, Binary Tree.

Companies

Asked at: Cisco, Samsung.