Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8491645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:29:15+00:00 2026-06-10T22:29:15+00:00

Given a BST with unique integers and a number K. Find a pair (

  • 0

Given a BST with unique integers and a number K. Find a pair ( a, b ) in BST such that
a + b = k.

Constraints:

You cannot change the structure of the tree, otherwise we could have converted it to sorted Doubly linked list and foiund the pair in O(N).

The approach should be in-place.

O(N) solution is needed.

I have thought of something related to running two pointers, one from left to right and the other from right from to left in the same way we do in case of finding pair in a sorted array. But, i could not get a clear picture of how to implement it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:29:16+00:00Added an answer on June 10, 2026 at 10:29 pm

    As Samuel already said I also think your solution should work.
    Two pointers or iterators, one from left to right (small to big) and one from right to left (big to small).
    If (a + b) > k then iterate the right to left one (the next smaller value) else the other one (the next bigger value).
    You can stop if a >= b
    The runtime is linear even in case of unbalanced tree.
    Every node is visited max one time.

    I think real function recursion will become somewhat complicated in this case.
    So better use two selfmade stacks to do the recursion in one function.
    Something like that:

    a = b = root;
    while (a.left) {
        astack.push(a)
        a = a.left
    }
    while (b.right) {
        bstack.push(b)
        b = b.right
    }
    
    
    while (a.value < b.value) {
        if (a.value + b.value == k) found!
        if (a.value + b.value < k) {
            if (a.right){
                a = a.right
                while (a.left) {
                    astack.push(a)
                    a = a.left
                }
            } else a = astack.pop()
        } else {
            if (b.left){
                b = b.left
                while (b.right) {
                    bstack.push(b)
                    b = b.right
                }
            } else b = bstack.pop()
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a binary search tree (BST). Find the maximum depth of the binary search
Find the largest number smaller than a given number(n) in the given BST. I
Given a BST, is it possible to find two numbers that add up to
Given a tree, how to find the centre node in the tree so that
Given the following simple BST definition: data Tree x = Empty | Leaf x
Suppose in a given binary tree if each node contains number of child elements
Given a node in a BST, how does one find the next higher key?
Given the following algorithm for inserting elements into BST : void InsertNode(Node* &treeNode, Node
Given that the web application doesn't have su privileges, I'd like to execute a
Given this: $ids = ''; I just realized that this: $single = $ids ==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.