Teaching Kids Programming – Two Sum in Binary Search Tree via Inorder and Two Pointer Algorithm


Teaching Kids Programming: Videos on Data Structures and Algorithms

Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target.

binary-search-tree Teaching Kids Programming - Two Sum in Binary Search Tree via Inorder and Two Pointer Algorithm algorithms math python teaching kids programming youtube video

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

Example 3:
Input: root = [2,1,3], k = 4
Output: true

Example 4:
Input: root = [2,1,3], k = 1
Output: false

Example 5:
Input: root = [2,1,3], k = 3
Output: true

Two Sum Algorithm in Binary Search Tree via Inorder and Two Pointer Algorithm

We can perform a inorder (LNR) on a binary search tree (BST) to get a sorted list. This requires O(N) time and space. Then we can perform a Two Pointer Algorithm (O(N) time) to find out if there are two numbers that sum up to target. The overall time is O(N) and the space complexity is O(N).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
        data = []
        def dfs(root):
            if not root:
                return
            dfs(root.left)
            data.append(root.val)
            dfs(root.right)
        dfs(root)
        i, j = 0, len(data) - 1
        while i < j:
            t = data[i] + data[j]
            if t == k:
                return True
            if t > k:
                j -= 1
            else:
                i += 1
        return False            
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
        data = []
        def dfs(root):
            if not root:
                return
            dfs(root.left)
            data.append(root.val)
            dfs(root.right)
        dfs(root)
        i, j = 0, len(data) - 1
        while i < j:
            t = data[i] + data[j]
            if t == k:
                return True
            if t > k:
                j -= 1
            else:
                i += 1
        return False            

Two Sum Variation Problems

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
404 words
Last Post: Teaching Kids Programming - Matrix Prefix Sum Algorithm
Next Post: Teaching Kids Programming - Finding All Paths from Source to Target in a Directed Acyclic Graph (DAG) using Recursive Depth First Search Algorithm

The Permanent URL is: Teaching Kids Programming – Two Sum in Binary Search Tree via Inorder and Two Pointer Algorithm

Leave a Reply