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 8333309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:59:05+00:00 2026-06-09T02:59:05+00:00

Given a binary search tree, in which any two nodes are swapped. So we

  • 0

Given a binary search tree, in which any two nodes are swapped. So we need to find those two nodes and swap them back(we need to swap the nodes, not the data)

I tried to do this by making an additional array which has the inorder traversal of the tree and also saves the pointer to each node.
Then we just traverse the array and find the two nodes that are not in the sorted order, now these two nodes are searched in the tree and then swapped

So my question is how to solve this problem in O(1) space ?

  • 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-09T02:59:07+00:00Added an answer on June 9, 2026 at 2:59 am

    In order traversal on a BST gives you a traversal on the elements, ordered.

    You can use an in-order traversal, find the two out of place elements (store them in two pointers) and swap them back.

    This method is actually creating the array you described on the fly, without actually creating it.

    Note that however, space consumption is not O(1), it is O(h) where h is the height of the tree, due to the stack trace. If the tree is balanced, it will be O(logn)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For a given binary tree, find the largest subtree which is also binary search
We are given a binary search tree; we need to find out its border.
Given a binary search tree (BST). Find the maximum depth of the binary search
Given a binary search tree and an integer K, i would like to find
Given two binary search trees, print the nodes in ascending order with time complexity
Given a binary search tree, i need to convert it into a doubly linked
I saw this problem: Given a binary search tree and a number, find a
This is a homework question, binary search has already been introduced: Given two arrays,
I wanted to know if a given binary tree is a binary search tree
I need to generate a series of N random binary variables with a given

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.