Given a Binary Search Tree (BST) and a positive integer k, find the k’th smallest element in the Binary Search Tree.
For example, in the following BST, if k = 3, then output should be 10, and if k = 5, then output should be 14.

We have discussed two methods in this post and one method in this post. All of the previous methods require extra space. How to find the k’th smallest element without extra space?
The idea is to use Morris Traversal. In this traversal, we first create links to Inorder successor and print the data using these links, and finally revert the changes to restore original tree. See this for more details.
Below is the implementation of the idea.
C++
#include<bits/stdc++.h>
using namespace std;
struct Node
{
int key;
Node *left, *right;
};
int KSmallestUsingMorris(Node *root, int k)
{
int count = 0;
int ksmall = INT_MIN;
Node *curr = root;
while (curr != NULL)
{
if (curr->left == NULL)
{
count++;
if (count==k)
ksmall = curr->key;
curr = curr->right;
}
else
{
Node *pre = curr->left;
while (pre->right != NULL && pre->right != curr)
pre = pre->right;
if (pre->right==NULL)
{
pre->right = curr;
curr = curr->left;
}
else
{
pre->right = NULL;
count++;
if (count==k)
ksmall = curr->key;
curr = curr->right;
}
}
}
return ksmall;
}
Node *newNode( int item)
{
Node *temp = new Node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
Node* insert(Node* node, int key)
{
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
return node;
}
int main()
{
Node *root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
for ( int k=1; k<=7; k++)
cout << KSmallestUsingMorris(root, k) << " " ;
return 0;
}
|
Java
import java.util.*;
class GfG {
static class Node
{
int key;
Node left, right;
}
static int KSmallestUsingMorris(Node root, int k)
{
int count = 0 ;
int ksmall = Integer.MIN_VALUE;
Node curr = root;
while (curr != null )
{
if (curr.left == null )
{
count++;
if (count==k)
ksmall = curr.key;
curr = curr.right;
}
else
{
Node pre = curr.left;
while (pre.right != null && pre.right != curr)
pre = pre.right;
if (pre.right== null )
{
pre.right = curr;
curr = curr.left;
}
else
{
pre.right = null ;
count++;
if (count==k)
ksmall = curr.key;
curr = curr.right;
}
}
}
return ksmall;
}
static Node newNode( int item)
{
Node temp = new Node();
temp.key = item;
temp.left = null ;
temp.right = null ;
return temp;
}
static Node insert(Node node, int key)
{
if (node == null ) return newNode(key);
if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
return node;
}
public static void main(String[] args)
{
Node root = null ;
root = insert(root, 50 );
insert(root, 30 );
insert(root, 20 );
insert(root, 40 );
insert(root, 70 );
insert(root, 60 );
insert(root, 80 );
for ( int k= 1 ; k<= 7 ; k++)
System.out.print(KSmallestUsingMorris(root, k) + " " );
}
}
|
Python3
class Node:
def __init__( self , data):
self .key = data
self .left = None
self .right = None
def KSmallestUsingMorris(root, k):
count = 0
ksmall = - 9999999999
curr = root
while curr ! = None :
if curr.left = = None :
count + = 1
if count = = k:
ksmall = curr.key
curr = curr.right
else :
pre = curr.left
while (pre.right ! = None and
pre.right ! = curr):
pre = pre.right
if pre.right = = None :
pre.right = curr
curr = curr.left
else :
pre.right = None
count + = 1
if count = = k:
ksmall = curr.key
curr = curr.right
return ksmall
def insert(node, key):
if node = = None :
return Node(key)
if key < node.key:
node.left = insert(node.left, key)
elif key > node.key:
node.right = insert(node.right, key)
return node
if __name__ = = '__main__' :
root = None
root = insert(root, 50 )
insert(root, 30 )
insert(root, 20 )
insert(root, 40 )
insert(root, 70 )
insert(root, 60 )
insert(root, 80 )
for k in range ( 1 , 8 ):
print (KSmallestUsingMorris(root, k),
end = " " )
|
C#
using System;
class GfG
{
public class Node
{
public int key;
public Node left, right;
}
static int KSmallestUsingMorris(Node root, int k)
{
int count = 0;
int ksmall = int .MinValue;
Node curr = root;
while (curr != null )
{
if (curr.left == null )
{
count++;
if (count==k)
ksmall = curr.key;
curr = curr.right;
}
else
{
Node pre = curr.left;
while (pre.right != null && pre.right != curr)
pre = pre.right;
if (pre.right == null )
{
pre.right = curr;
curr = curr.left;
}
else
{
pre.right = null ;
count++;
if (count == k)
ksmall = curr.key;
curr = curr.right;
}
}
}
return ksmall;
}
static Node newNode( int item)
{
Node temp = new Node();
temp.key = item;
temp.left = null ;
temp.right = null ;
return temp;
}
static Node insert(Node node, int key)
{
if (node == null ) return newNode(key);
if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
return node;
}
public static void Main(String[] args)
{
Node root = null ;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
for ( int k = 1; k <= 7; k++)
Console.Write(KSmallestUsingMorris(root, k) + " " );
}
}
|
Javascript
<script>
class Node {
constructor() {
this .key = 0;
this .left = null ;
this .right = null ;
}
}
function KSmallestUsingMorris(root , k)
{
var count = 0;
var ksmall = Number.MIN_VALUE;
var curr = root;
while (curr != null )
{
if (curr.left == null )
{
count++;
if (count==k)
ksmall = curr.key;
curr = curr.right;
}
else
{
var pre = curr.left;
while (pre.right != null && pre.right != curr)
pre = pre.right;
if (pre.right== null )
{
pre.right = curr;
curr = curr.left;
}
else
{
pre.right = null ;
count++;
if (count==k)
ksmall = curr.key;
curr = curr.right;
}
}
}
return ksmall;
}
function newNode(item)
{
var temp = new Node();
temp.key = item;
temp.left = null ;
temp.right = null ;
return temp;
}
function insert(node , key)
{
if (node == null ) return newNode(key);
if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
return node;
}
var root = null ;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
for (k=1; k<=7; k++)
document.write(KSmallestUsingMorris(root, k) + " " );
</script>
|
Output20 30 40 50 60 70 80
Time Complexity: O(n) where n is the size of BST
Auxiliary Space: O(1)
This article is contributed by Abhishek Somani. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above