2-Sum Binary Tree

Given a binary search tree T, where each node contains a positive integer, and an integer K, you have to find whether or not there exist two different nodes A and B such that A.value + B.value = K.

Return 1 to denote that two such nodes exist. Return 0, otherwise.

Notes

  • Your solution should run in linear time and not take memory more than O(height of T).
  • Assume all values in BST are distinct.

Example :

Input 1: 

T :       10
         / \
        9   20

K = 19

Return: 1

Input 2: 

T:        10
         / \
        9   20

K = 40

Return: 0
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving 2-Sum Binary Tree on Interview Code Editor
Sign Up
to access hints and editorial solutions for 2-Sum Binary Tree
Asked In:
  • Amazon
Click here to start solving coding interview questions