Find a pair with given target in BST

The expected space complexity for this question is O(height of tree), but I am getting O(n) solution. I have seen people using inorder traversal to do this , but that too takes O(n) space…PLEASE HELP

Problem Link : Find a pair with given target in BST | Practice | GeeksforGeeks

Just try to use a two pointer technique as is done in arrays, keep one pointer at the minimum value of the bst and other pointer at the maximum value of the bst then move towards the the root node.
Just make sure to store the nodes and not just the data.

1 Like