Given two linked lists, where linked list represents a big number and each node of linked list contains one digit of the number. Problem is to add two numbers represented by linked lists. The result should be stored in a third linked list. It should be noted that the head node contains the most significant digit of the numbers.

For example. let’s say we have been given two numbers: 12563 and 56743 and we have to add them. Two linked lists which will represent these numbers are as shown below.

This problem is very easy to solve if the order of the digits stored in the linked list is reversed. Look at the code given at leetcode.

``` public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry =0;

ListNode p1 = l1, p2 = l2, p3=newHead;

while(p1 != null || p2 != null){
if(p1 != null){
carry += p1.val;
p1 = p1.next;
}

if(p2 != null){
carry += p2.val;
p2 = p2.next;
}

p3.next = new ListNode(carry%10);
p3 = p3.next;
carry /= 10;
}

if(carry==1)
p3.next=new ListNode(1);

}
```

However, linked lists are not stored in reversed order and basic mathematics tells us that addition starts from the least significant digit. To access least significant digit of the numbers, we will visit the last nodes of both the lists and add them up, create a new node to store the result, take care of the “carry” if any.

Next, we go to the second to last node and do the same. Link the result node to the result node which was created when we added the last nodes.

This will continue  backwards till we reach head of one the list. Once, we reach head of a linked list, we just add all the nodes of the remaining list to the result.

It is clear that we go to the end of the lists and move back one node at a time. Actually, this is a reverse traversal of a linked list, which can easily be done with recursion, function call stack will automatically store previous nodes. However, we need to take into account the difference in the number of digits in two number and hence the difference in length of the two lists.

So before starting recursion, calculate the size difference and move the longer list pointer to appropriate place so that we reach the last node of both lists at the same time.

Another thing is to take care of is carry. If two digits add more than 10, forward the carry to the next node and add it to it. If most significant digit addition results in a carry, create an extra node to store carry.

```#include<stdlib.h>
#include<stdio.h>

typedef struct node{
int data;
struct node *next;
} Node;

int length( Node * head ){
int len = 0;
while(current){
len++;
current = current->next;
}
return len;
}

Node * createNode(int value){

Node * newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;

return newNode;

}

Node *newNode = createNode (value);
}
else{
}
}
/* This function is actually helper function
which does all house keeping like calculating
lengths of lists,calling recursive implementation,
creating extra node for carry in MSD,
and adding any remaining nodes left in longer list. */

/* result is pointer to pointer to the head of resulting node */
void addTwoNumbers(Node *L1, Node *L2, int *carry, Node  **result)
{
int len1 = length( L1 );
int len2 = length( L2 );
int diff = 0;

if(len1 < len2){
Node * current = L1;
L1 = L2;
L2 = current;
}
diff = abs(len1-len2);
Node * current = L1;

while(diff--)
current = current->next;

/* Call the recursive implementation */

diff = abs(len1-len2);

/* Add remaining nodes in longer list */

if(*carry){
push(result, *carry);
}
return;
}
int *carry, Node **result){

int sum;
if(!L1)
return;

/*We have reached the last node of both lists, add them */
sum = L1->data + L2->data + (*carry);

int value = sum%10;
*carry = sum/10;
push(result, value);

return;
}
Node **result, int diff){
int sum =0;

if(!L1 || !diff)
return;

sum = L1->data + (*carry);
int value = sum%10;
*carry = sum/10;

push(result, value);

return;
}

void printList( Node * head ){
while(current){
printf("%d ->", current->data);
current = current->next;
}
printf("NULL");
}
/* Driver program to run above code */
int main(){
Node * L1 = NULL;
Node * L2 = NULL;
Node * result = NULL;
int carry = 0 ;
/* creating list 1 */
push(&L1,3);
push(&L1,4);
push(&L1,6);
push(&L1,7);
/* creating list 2 */
push(&L2,8);
push(&L2,9);
push(&L2,7);

printList(L1);
printf("\n");
printList(L2);

printf("\n");
printList(result);
return 0;
}
```

Since we traverse the number of nodes in the long list, the complexity of the algorithm would be O(n), where n is the number of nodes in the long list. Also, we need n extra nodes to store the result. Hence space complexity to add two numbers represented by linked lists is O(n).

# Binary tree to doubly linked list conversion

This is very commonly asked problem in Amazon and Google interview question. It checks your understanding of binary tree and doubly link list data structures, their traversals and creation. In a binary tree, a node has two pointers, one point to the left child of the node and other points to the right child of the node. Similarly, in a doubly linked list, a node contains two pointers, one point to the next node and other points to the previous node of the current node. Given a binary tree, how can we convert the binary tree to a doubly linked list? To make the problem clear, let’s take an example.

Output should be

### Binary tree to a doubly linked list: Line of thought

The first question you should be asking is which node should be the head of the doubly linked list? If the leftmost node in the binary tree is to be head, you need to traverse a tree in inorder. However, if the root node has to be the head node, then you should do preorder traversal.

This question could be great started as it shows your understanding of tree traversals.

Let’s say our problem is to have inorder traversal. If a binary tree is binary search tree, the interviewer can just tell you to provide the output as a sorted list, which is another way of saying inorder traversal.

To convert a binary tree to a doubly linked list, at each node, the previous pointer will point to the inorder predecessor of the node whereas next pointer points to inorder successor of the node. We have left and right pointers already in the node, use them as previous and next pointer respectively.
Now, problem is to keep track of inorder successor and predecessor of the node. To keep track of inorder predecessor, store the previous node of the current node visited throughout traversal. We would link the left child to the inorder predecessor.
How to keep find the inorder successor of a node, if the node is left child of some node, the inorder successor would be parent node. At the parent node, it would be the leftmost child of the right child if it exists. This is not scary as it sound, inorder successor part is automatically implemented if we do the recursive inorder traversal of the tree.

There is one special case to handle, which is the leftmost node. This node will be head of the doubly linked list and there is no previous node to link it’s left to.

This whole problem is a complex version of inorder traversal of a binary tree, but at the end, it is inorder traversal. We can first write the generic inorder traversal and then modify the process step to suit our needs.

```    private void inorder(Node root){
if(root == null) return;

if(root.left != null) inorder(root.left);
process(root);
if(root.right != null) inorder(root.right);
}
```

We have to modify this to send previous node, where root connects it’s left child in process step and head, which is set only once, to store the head of result doubly linked list.

```private void inorder(Node root, Node prev, Node head){
if(root == null) return;

if(root.left != null) inorder(root.left);
if(root.right != null) inorder(root.right);
}
```

What we have to do in process step? If the current node is the leftmost node, we will set that head of the doubly linked list. If the previous node is null yet, that means, we have it the leftmost node.
What if the previous node is not null, in that case, we have already created or assigned head of the doubly linked list, and the previous node points to the last node of the doubly linked list. It means we have to link right pointer of the previous node to the current node.
Set left pointer of the current node to the previous node irrespective of the previous node. The last step would update the previous with the current node.

```private void process(TreeNode root, TreeNode prev, TreeNode head){
if(prev == null){
}
else{
prev.setRight(root);
}
root.setLeft(prev);
prev = root;
}
```

The only problem is that if we are implementing the process function Java, we cannot change the value of pointer because Java passes arguments as pass-by-value rather than pass-by-reference. In short, even though head and previous pointers are updated in process function, the change would not be reflected in the calling function. To handle this, we will create a mutable static class with two members called head and previous and pass it to process function. Why this method works, please refer  :  pass-by-value and pass-by-reference in java

```    private void process(TreeNode root, Context context){
if(context.prev == null){
}
else{
context.prev.setRight(root);
}

root.setLeft(context.prev);
context.prev = root;
}
```

### An algorithm to convert a binary tree to a doubly linked list

1. Start from root node, currentNode.
2. If currentNode->left != NULL, currentNode = currentNode->left. (Moving down the tree on the left side)
3. At this point we are at the left most node, check if previous == NULL.
1. If previous is NULL, then this node has to be the head node.
2. Mark currentNode as head and currentNode->left = previous = NULL.
4. previous = currentNode.
5. Traverse up the tree in inorder traversal.
1. previous->right = currentNode.
2. currentNode->left = previous
3. previous = currentNode
6. If currentNode->right != NULL, currentNode = currentNode->right and go to step 2.

#### Convert Tree to DLL : implementation in Java

```package com.company.BST;

/**
* Created by sangar on 21.10.18.
*/
public class TreeToDLL {

public static class Context {
public TreeNode prev;
}

private void process(TreeNode root, Context context){
if(context.prev == null){
}
else{
context.prev.setRight(root);
}

root.setLeft(context.prev);
context.prev = root;
}

public void treeToDllRecursion(TreeNode root, Context context){

if(root == null) return;

if(root.getLeft() != null) {
treeToDllRecursion(root.getLeft(),context);
}

process(root,context);

if(root.getRight() != null)
treeToDllRecursion(root.getRight(), context);
}
}
```

Definition of TreeNode and BinarySearchTree is given below.

```package com.company.BST;

/**
* Created by sangar on 21.10.18.
*/
public class TreeNode<T> {
private T value;
private TreeNode left;
private TreeNode right;

public TreeNode(T value) {
this.value = value;
this.left = null;
this.right = null;
}

public T getValue(){
return this.value;
}
public TreeNode getRight(){
return this.right;
}
public TreeNode getLeft(){
return this.left;
}

public void setValue(T value){
this.value = value;
}

public void setRight(TreeNode node){
this.right = node;
}

public void setLeft(TreeNode node){
this.left = node;
}
}
```
```package com.company.BST;

/**
* Created by sangar on 10.5.18.
*/
public class BinarySearchTree<T> {

private TreeNode<T> root;

public void BinarySearchTree(){
root = null;
}

public void insert(int value){
this.root =  insertNode(this.root, value);
}

private TreeNode insertNode(TreeNode root, int value){
if(root == null){
//if this node is root of tree
root = new TreeNode(value);
}
else{
if((int)root.getValue() > value){
/*If root is greater than value,
node should be added to left subtree */
root.setLeft(insertNode(root.getLeft(), value));
}
else{
/*If root is less than value,
node should be added to right subtree */
root.setRight(insertNode(root.getRight(), value));
}
}
return root;
}

public TreeNode getRoot(){
return this.root;
}

public void setRoot(TreeNode node){
this.root = node;
}
}
```

#### Binary search tree to DLL conversion : C implementation

```#include<stdio.h>
#include<stdlib.h>
#include<math.h>

struct node{
int value;
struct node *left;
struct node *right;
};
typedef struct node Node;

void treetoListRec(Node * node, Node ** prev, Node **ptrToHead){
if(node == NULL)
return;
/* Go to left most child */
if(node->left)

/* If this wasn't the first node being added to list*/
if(*prev!= NULL){
(*prev)->right = node;
}
else{
}
/*make left pointer point to last node, and update the
last node to current*/

node->left = *prev;
*prev= node;

/* If there is right child, process right child */
if(node->right)

}
Node * createNode(int value){
Node * newNode=  (Node *)malloc(sizeof(Node));
newNode->value = value;
newNode->right = NULL;
newNode->left = NULL;
return newNode;
}
Node * addNode(Node *node, int value){
if(!node)
return create_node(value);
else{
if (node->value > value)
else
}
return node;
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;
Node * prev = NULL;
//Creating a binary tree

return 0;
}
```

This requires traversal of each node at least once, hence complexity analysis is O(N).

#### Note

There is one method , which takes into consideration that the whole problem can be divided into sub-problems involving left sub tree and right sub tree, once these sub problems are solved, we can combine solutions of these to come up with the solution of the bigger problem.

Basic idea is to convert left sub binary tree to doubly linked list, then convert right sub binary tree to doubly linked list and join both the lists with root in between. Idea is very well explained here

Please share if there is something missing or wrong. If you want to contribute to website, please reach out to us on [email protected]

# Level order traversal of a binary tree

Given a binary tree, print all the nodes of binary tree level-wise, this is another way of saying perform a breadth-first traversal of the binary tree and known as level order traversal of a binary tree. What is level order traversal of a binary tree? It means that nodes at a given level are printed before all the levels below it. For example, level order traversal of below tree would be [10,7,15,8,9,18,19]

## Level order traversal: Thoughts

In a binary tree, each node has two children, when we process a node, we can already know what is at the next level. As the leftmost child at a given level has to be printed first, followed by its right sibling then by its cousins. If we store all the nodes at the next level from left to right, which data structure is best for this use case? It’s First In First Out pattern, hence the queue. Let’s see how it works with an example.

Start with root node and enqueue the root node in the queue.

Now, while the queue is not empty,  dequeue node from the queue and push it’s left and right children onto queue if they exist. In this case, we will dequeue node(10) from the queue and enqueue node(7) and node(15) to queue. Output till now is 10.

Again, dequeue from the queue, node(7) and store it in output. At the same time, store it’s left child node(8) and right child node(9) to queue. The output is [10, 7]

Now, dequeue node(15) from the queue, put it on to output and enqueue it’s left child node(18) and right child node(19) on to the queue. The output is [10,7,15].

Again, node(8) is dequeue from the queue and put on to the output, as there are no left and right child of node(8), nothing is enqueued in the queue. Same is true for all the nodes in the queue. We continue till queue is empty. Final output will be [10,7,15,8,9,,18,19].

### Level order traversal of binary tree : implementation

```    public ArrayList<Integer> levelOrderTraversal(TreeNode root){

ArrayList<Integer> traversal = new ArrayList<>();

if(root == null) return traversal;

while (!queue.isEmpty()){
TreeNode current = queue.poll();

if(current.getLeft()!= null)
if(current.getRight()!= null)
}
return traversal;
}
```

As each node of the binary tree is visited at least once, time complexity  `O(n)` along with space complexity of `O(2(l-1))` where l is the number of levels in the binary tree.

The method explained above has an additional space complexity, is there a way to avoid that?  To print all the nodes on a particular level, first of all, we must know the number of levels in the binary tree, which is nothing but the height of the tree.

Start with level 0 and print all nodes on level 0, then move to level 1 and print all the nodes at level. To reach cousins of a node, we have to come back to the parent of the parent node of the current node. How about we always start at the root node with the desired level to be printed? If the node is at the desired level, print it and start again from the root.

Implementation wise it’s simple recursive function, where we pass the desired level to be printed, at each recursive call, the desired level decreases by 1. When the desired level is 1, print the node as that node will be at the level we are printing currently.

• Find the height of the tree.
• For each level:
• Start from the root for each level.
• Decrement the level count while moving to the left and right child.
• If the level count is 1, print the node.
• Else move down to left subtree and right subtree.

This algorithm is more useful when you have to print a specific level of binary tree and not all. Complexity of this method to do  level order traversal of binary tree is O(n log n).

#### Level order traversal: recursive implementation

```    public ArrayList<Integer>
levelOrderTraversalRecursive(BinarySearchTree tree){
ArrayList<Integer>traversal = new ArrayList<>();

int height = tree.height();

for(int i=1; i>=height; i++){
traverseLevel(tree.getRoot(), i, traversal);
}
return traversal;
}

private void traverseLevel(TreeNode root, int level,
ArrayList<Integer> levelTraversal){
if(level == 1){
}

if(root.getLeft() != null)
traverseLevel(root.getLeft(), level-1, levelTraversal);
if(root.getRight() != null)
traverseLevel(root.getRight(), level-1,levelTraversal);
}
```

Definition of binary tree and tree node is as follows.

```package com.company.BST;

/**
* Created by sangar on 10.5.18.
*/
public class BinarySearchTree<T> {

private TreeNode<T> root;

public void BinarySearchTree(){
root = null;
}

public void insert(int value){
this.root =  insertNode(this.root, value);
}
private TreeNode insertNode(TreeNode root, int value){
if(root == null){
//if this node is root of tree
root = new TreeNode(value);
}
else{
if((int)root.getValue() > value){
/* If root is greater than value,
node should be added to left subtree */
root.setLeft(insertNode(root.getLeft(), value));
}
else{
/* If root is less than value,
node should be added to right subtree */
root.setRight(insertNode(root.getRight(), value));
}
}
return root;
}

public TreeNode getRoot(){
return this.root;
}

public void setRoot(TreeNode node){
this.root = node;
}

public int height(){
return height(this.getRoot());
}

private int height(TreeNode currentNode){
if(currentNode == null) return 0;

int lh = height(currentNode.getLeft());
int rh = height(currentNode.getRight());

return 1 + Integer.max(lh, rh);
}
}
```
```package com.company.BST;

/**
* Created by sangar on 21.10.18.
*/
public class TreeNode<T> {
private T value;
private TreeNode left;
private TreeNode right;

public TreeNode(T value) {
this.value = value;
this.left = null;
this.right = null;
}

public T getValue(){
return this.value;
}
public TreeNode getRight(){
return this.right;
}
public TreeNode getLeft(){
return this.left;
}

public void setValue(T value){
this.value = value;
}

public void setRight(TreeNode node){
this.right = node;
}

public void setLeft(TreeNode node){
this.left = node;
}
}

```

Please share if there is something wrong or missing. If you want to take personalized coaching from our expert teachers, please signup for a free session.

# Print paths in binary search tree

Print paths in binary search tree from root to leaf node. There can be many paths in a tree. AT every node, there are two paths splitting. Maximum length of path in binary search tree can be N nodes. Consider following tree:

Paths in given binary search tree are : [ 10,5,1 ], [10,5,6 ], [10,19,17 ], [10,19,20]

## Paths in binary search tree : Algorithm

As every path start with root node, it’s obvious that first node in every path with root node.  At root node, we have two choices. We can go to left subtree or can go to right subtree. It does not matter what subtree we go first as we have to traverse all paths anyway.

Let’s say we traverse left subtree first. At this point, we have first node in path, all we need to find all paths in left subtree and append those path with first node we have.
At first left child, problem remains the same, find paths in binary search tree. Additional step is to append to existing path we have already have covered. Are you getting a hint on recursive nature of problem?

Once, we have traversed all paths on left subtree, it’s time to traverse all paths on right subtree.

To keep track of nodes which are already added to path, we have to maintain a list of nodes, which will be updated whenever we move up and down in BST.

As mentioned, path ends on leaf node, as soon as we hit a node which has no left or right child, start going back up in the tree. In implementation terms, this will be termination condition for recursive function. Algorithm to print paths in binary tree.

### Paths in binary search tree : Implementation

```#include<stdio.h>
#include<stdlib.h>

struct node{
int value;
struct node *left;
struct node *right;
};

typedef struct node Node;

void printPaths(Node * node, int path[], int pathLen){

int i;

if(!node)
return;

path[pathLen]  = node->value;

int isLeaf = ! ( node->left || node->right ) ;
if(isLeaf ){
printf("\n Path till node %d is :", node->value);
for(i=0; i<=pathLen; i++){
printf("%d, ", path[i]);
}
}

printPaths(node->left,  path, pathLen+1);
printPaths(node->right, path, pathLen+1);

return ;
}

Node * createNode(int value){
Node *newNode =  (Node *)malloc(sizeof(Node));

newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;

}

Node * addNode(Node *node, int value){
if(node == NULL){
return createNode(value);
}
else{
if (node->value > value){
}
else{
}
}
return node;
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;
int n = 0;
//Creating a binary tree
int path[100];
printPaths(root, path, 0);

return 0;
}
```

Java implementation

```package com.company.BST;

import java.util.ArrayList;

/**
* Created by sangar on 10.5.18.
*/
public class BinarySearchTree {

private Node root;

public void BinarySearchTree(){
root = null;
}

public class Node {
private int value;
private  Node left;
private Node right;

public Node(int value) {
this.value = value;
this.left = null;
this.right = null;
}
}

public void insert(int value){
this.root =  insertNode(this.root, value);
}

private Node insertNode(Node root, int value){
if(root == null){
//if this node is root of tree
root = new Node(value);
}
else{
if(root.value > value){
//If root is greater than value, node should be added to left subtree
root.left = insertNode(root.left, value);
}
else{
//If root is less than value, node should be added to right subtree
root.right = insertNode(root.right, value);
}
}
return root;
}

public void printPath(){
ArrayList<Node> path  = new ArrayList<>();
this.printPathRecursive(this.root, path);
}

private void printPathRecursive(Node root, ArrayList<Node> path){
if(root == null) return;

//If node is leaf node
if(root.left == null && root.right == null){
path.forEach(node -> System.out.print(" " + node.value));
path.remove(path.size()-1);
System.out.println();
return;
}

printPathRecursive(root.left,path);
printPathRecursive(root.right, path);

path.remove(path.size()-1);

}
}
```

Test class

```package com.company.BST;

/**
* Created by sangar on 10.5.18.
*/
public class BinarySearchTreeTests {
public static void main (String[] args){
BinarySearchTree binarySearchTree = new BinarySearchTree();

binarySearchTree.insert(7);
binarySearchTree.insert(8);
binarySearchTree.insert(6);
binarySearchTree.insert(9);
binarySearchTree.insert(3);
binarySearchTree.insert(4);

binarySearchTree.printPath();
}
}
```

Since we are traversing each node at least once, complexity of  implementation for print all paths in binary search tree is  O(n) where n is number of nodes.

Please share if there is something wrong or missing. If you want to contribute and share your knowledge with thousands of learners across world, please reach out to us at [email protected]

## Check if tree is BST or not

This is one of the most asked programming interview questions. How to check or validate that a given binary tree is BST or not or if a given tree is a binary search tree? For example, the first and second binary trees are BST but not the third one.

In binary tree introduction  we touched upon the topic of recursive structure of binary search tree.  The first property to satisfy to be qualified as BST is: value in all nodes on the left subtree of the root node are smaller and the value of all nodes in the right subtree is greater than the root node. This property should be valid at all nodes.

## Check if binary tree is (BST) or not: Recursive implementation

So, to see if the binary tree rooted a particular node is BST, the root is greater than all nodes on the left subtree and less than all nodes on the right subtree. However, is it sufficient condition? Let’s take a counterexample and prove that even root is greater than all nodes on the left side and smaller than all nodes on the right subtree, a binary tree may not be binary search tree. Look at the tree below.

In this tree above condition is satisfied, but we cannot call this binary tree a BST.

This is a recursive structure of a binary search tree that plays an important role. For a binary tree root at a node to be BST, it’s left subtree and right subtree should also be BST. So, there are three conditions which should be satisfied:

1. Left subtree is BST
2. Right subtree is BST
3. Value of root node is greater than the max in the left subtree and less than minimum in right subtree

### Check if binary tree is (BST) or not  : Recursive implementation

```#include<stdio.h>
#include<stdlib.h>

#define true 1
#define false 0

struct node{
int value;
struct node *left;
struct node *right;
};

typedef struct node Node;

Node * findMaximum(Node * root){
if( !root ) return root;
while( root->right ){
root = root->right;
}
return root;
}

Node * findMinimum(Node * root){
if( !root ) return root;
while( root->left ){
root = root->left;
}
return root;
}

int isBST(Node * node){

if(!node)
return true;

if( ! ( node->left || node->right ) ) return true;
int isLeft  = isBST(node->left);
int isRight = isBST(node->right);

if(isLeft && isRight){
/* Since we already know that left sub tree and
right sub tree are Binary search tree, finding min and max in them would be easy */

Node *max  =  NULL;
Node *min  =  NULL;
if( node->left )
max = findMaximum(node->left);
if( node->right )
min = findMinimum(node->right);

//Case 1 : only left sub tree is there
if(max && !min)
return node->value > max->value;
//Case 2 : Only right sub tree is there
if(!max && min)
return node->value < min->value;
//Case 3 : Both left and right sub tree are there
return (node->value > max->value && node->value < min->value);
}
return false;
}

Node * createNode(int value){
Node *newNode =  (Node *)malloc(sizeof(Node));

newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;
}

Node * addNode(Node *node, int value){
if(node == NULL){
return createNode(value);
}
else{
if (node->value < value){
}
else{
}
}
return node;
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;
//Creating a binary tree

printf("%s", isBST(root ) ? "Yes" : "No" );

return 0;
}
```

#### Check if binary tree is (BST) or not  : Optimized implementation

Above implementation to check if the binary tree is binary search tree or not is correct but inefficient because, for every node,  its left and right subtree are scanned to find min and max. It makes implementation non-linear.

How can we avoid re-scanning of left and right subtrees? If we can keep track max on left subtree and min on right subtree while checking those subtrees for BST property and use the same min and max.

Start with INTEGER_MAX and INTEGER_MIN, check if the root node is greater than max and less than min. If yes, then go down left subtree with max changed to root value, and go down to right subtree with min changed to root value. It is a similar implementation as above, except revisiting nodes.

```#include<stdio.h>
#include<stdlib.h>

#define true 1
#define false 0
#define INT_MIN -32999
#define INT_MAX 32999

struct node{
int value;
struct node *left;
struct node *right;
};

typedef struct node Node;

int isBSTHelper(Node *root, int max, int min){
if(!root) return true;

if(root->value < min || root->value > max){
return false;
}

return isBSTHelper(root->left, root->value, min) &&
isBSTHelper(root->right, max, root->value);
}

int isBST(Node *root){
return isBSTHelper(root, INT_MAX, INT_MIN);
}

Node * createNode(int value){
Node *newNode =  (Node *)malloc(sizeof(Node));

newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;
}

Node * addNode(Node *node, int value){
if(node == NULL){
return createNode(value);
}
else{
if (node->value < value){
}
else{
}
}
return node;
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;
//Creating a binary tree

printf("%s", isBST(root ) ? "Yes" : "No" );

return 0;
}
```

The complexity of the above implementation is O(n) as we are traversing each node only once.

Another method to see if the binary tree is BST or not is to do an inorder traversal of the binary tree and keep track of the previous node. As we know in-order traversal of a binary search tree gives nodes in sorted order, previously visited node should be always smaller than the current node. If all nodes satisfy this property, a binary tree is a binary search tree. If this property is violated at any node, the tree is not a binary search tree.

The complexity of this implementation is also O(n) as we will be traversing each node only once

Please share if there is something missing or not correct. If you want to contribute and share your knowledge with thousands of learner around the world, please reach out to us at [email protected]

## Closest element in a binary search tree

The problem statement is: given a binary search tree and a value, find the closest element to that value in the binary search tree. For example, if below is the binary search tree and value given is 16, then the function should return 15.

### Closest element in BST: thoughts

A simple approach is to go to all nodes of BST and find out the difference between the given value and value of the node. Get the minimum absolute value and add that minimum value from the given value, we would get the closest element in the BST.

Do we really need to scan all the nodes? No we don’t need to. Let’s say given value is k.

What if current.value is equal to the k? That means it is the closest node to the k and we cannot go better than this. Return the node value as closest value.

If current.value is greater than k, by virtue of BST, we know that nodes on the right subtree of the current node would be greater than the current.value. Hence the difference between k and all the nodes on the right subtree would only increase. So, there is no node on the right side, which is closer than the current node itself. Hence, the right subtree is discarded. If there is no left subtree, return current.value.

If current.value is less than k,  with the same logic above, all elements in left subtree are less than the value of current.value, the difference between k and the nodes on left subtree would only increase. So, we can safely discard the left subtree. If there is no right subtree, return current.value.

When we return the closest element from left subtree, we check if the difference between current.value and k less than difference between returned value and k. If it is less, then return current.value else return the returned value from the subtree.

### Closest element in a binary search tree: algorithm

1. If `k == current.value`, return current.value.
2. If `k < current.value`, search the closest element in left subtree including the root as current is still a candidate.
1. Return current.value if there is no left subtree.
3. If `k > current.value`, search the closest element in the right subtree.
1. Return current.value if there is no right subtree.
4. If `abs(current.value - k ) < abs(returnedValue - k )`, return current.value else return returnedValue

#### Closest element in binary search tree: implementation

```package com.company.BST;

/**
* Created by sangar on 3.11.18.
*/
public class ClosestElement {

public int findClosest(TreeNode node, int k){
if(node == null) return -1;

int currentValue = (int)node.getValue();

if(currentValue == k) return currentValue;

if(currentValue > k){
if(node.getLeft() == null) return currentValue;

//Find closest on left subtree
int closest = findClosest(node.getLeft(), k);
if(Math.abs(closest - k) > Math.abs(currentValue - k)){
return currentValue;
}
return closest;
}
else {
if (node.getRight() == null) return currentValue;

//Find closest on right subtree
int closest = findClosest(node.getRight(), k);
if (Math.abs(closest - k)
> Math.abs(currentValue - k)) {
return currentValue;
}
return closest;
}
}
}
```
```package com.company.BST;

/**
* Created by sangar on 21.10.18.
*/
public class TreeNode<T> {
private T value;
private TreeNode left;
private TreeNode right;

public TreeNode(T value) {
this.value = value;
this.left = null;
this.right = null;
}

public T getValue(){
return this.value;
}
public TreeNode getRight(){
return this.right;
}
public TreeNode getLeft(){
return this.left;
}

public void setValue(T value){
this.value = value;
}

public void setRight(TreeNode node){
this.right = node;
}

public void setLeft(TreeNode node){
this.left = node;
}
}

```
```package test;

import com.company.BST.BinarySearchTree;
import com.company.BST.ClosestElement;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Created by sangar on 23.9.18.
*/
public class ClosestElementTest {

ClosestElement tester = new ClosestElement();

@Test
public void closestElementTest() {

BinarySearchTree<Integer> binarySearchTree =
new BinarySearchTree<>();
binarySearchTree.insert(10);
binarySearchTree.insert(8);
binarySearchTree.insert(15);
binarySearchTree.insert(12);
binarySearchTree.insert(6);
binarySearchTree.insert(9);

assertEquals(6,
tester.findClosest(binarySearchTree.getRoot(),1));
}
}

```

Complexity of algorithm to find closest element in a binary search tree is O(n) if tree is completely skewed.

## Two nodes with a given sum in a binary search tree

Given a binary search tree and an integer k, find all two nodes with given sum, nodes (a,b) such that a+b =k. In other words, find two nodes in a binary search tree which add to a number. For example, in the binary search tree given below, if k = 24, node(5) and node(19) should be the result.

### Two nodes with given sum in binary search tree: thoughts

We have solved a similar problem with arrays, where we had to find pairs with a given sum k, the basic idea was that if the array is sorted, we will scan it from start(moving right) and end(moving left), and see if elements at two ends add up to k. If the sum of those numbers is greater than k, we move to right from start. If the sum of those two numbers is less than k, we move to left from end. We continue till two pointers cross each other.

Is there a way in which binary search tree can be sorted array? Yes, if BST is traversed in inorder, output is in sorted order of nodes. So, if we scan the BST and store in an array, the problem is same as find pairs in an array with a given sum.
However, the solution requires two scans of all nodes and additional space of O(n).

Another solution could be to use a hash map. Each node is stored in the hashmap and at each node, we check if there is a key (sum-node.value) present in the hashmap. If yes, then two nodes are added into the result set. This still requires additional space, however, there is only one traversal of the tree.

How can we avoid additional space? Look at the binary search tree property: all the nodes on the left subtree of a root node are less than and all the nodes on right subtree are greater than the root node.
We know that the minimum node in BST is the leftmost node and the maximum node is the rightmost node.
If we start an inorder traversal, the leftmost node is the first node to be visited and if we do a reverse inorder traversal, the rightmost node is the first node will be visited. If the sum of the minimum and the maximum is less than k, then we have to go to the second minimum (next node in forward inorder traversal). Similarly, if the sum of the minimum and the maximum is greater than k, then we have to go to the second maximum(next in reverse inorder traversal)

How can we do a forward and reverse inorder traversal at the same time?

### Sum of two nodes in binary search tree: stacks

We know how to traverse a tree using stack data structure. We will use two stacks, one stack stores the nodes to be visited in forward inorder traversal and second stores the nodes to be visited in reverse inorder traversal. When we reach the leftmost and the rightmost node, we pop from the stacks and check for equality with the given sum.
If the sum of the two nodes is less than k, we increase one of the nodes. We will go to the right subtree of the node popped from the forward inorder stack. Why? Because that’s where we will find the next greater element.

If the sum of the two nodes is greater than k, we decrease one of the nodes. We will go to the left subtree of the node popped from the reverse inorder stack. Why? Because that’s where we will find the next smaller element.

We will continue till both forward and reverse inorder traversal do not meet.

#### Two nodes with given sum in BST: Implementation

```package com.company;

import com.company.BST.TreeNode;
import javafx.util.Pair;

import java.util.ArrayList;
import java.util.Stack;

/**
* Created by sangar on 26.11.18.
*/
public class TwoNodesWithGivenSum {
public ArrayList<Pair<Integer, Integer>>
findPairsWithGivenSum(TreeNode root, int sum) {

Stack<TreeNode> s1 = new Stack<>();
Stack<TreeNode> s2 = new Stack<>();

ArrayList<Pair<Integer, Integer>> result
= new ArrayList<>();

TreeNode cur1 = root;
TreeNode cur2 = root;

while (!s1.isEmpty() || !s2.isEmpty() ||
cur1 != null || cur2 != null) {
if (cur1 != null || cur2 != null) {
if (cur1 != null) {
s1.push(cur1);
cur1 = cur1.getLeft();
}

if (cur2 != null) {
s2.push(cur2);
cur2 = cur2.getRight();
}
} else {
int val1 = (int)s1.peek().getValue();
int val2 = (int)s2.peek().getValue();

if (s1.peek() == s2.peek()) break;

if (val1 +  val2 == sum)

if (val1 + val2 < sum) {
cur1 = s1.pop();
cur1 = cur1.getRight();
} else {
cur2 = s2.pop();
cur2 = cur2.getLeft();
}
}
}

return result;
}
}

```

Test cases

```package test;

import com.company.BST.BinarySearchTree;
import com.company.TwoNodesWithGivenSum;
import javafx.util.Pair;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Created by sangar on 23.9.18.
*/
public class TwoNodesWithGivenSumTest {

TwoNodesWithGivenSum tester = new TwoNodesWithGivenSum();

@Test
public void twoNodesWithGivenSumTest() {

BinarySearchTree<Integer> binarySearchTree
= new BinarySearchTree<>();
binarySearchTree.insert(10);
binarySearchTree.insert(8);
binarySearchTree.insert(15);
binarySearchTree.insert(12);
binarySearchTree.insert(6);
binarySearchTree.insert(9);

ArrayList<Pair<Integer, Integer>> result
= new ArrayList<>();

assertEquals(result,
tester.findPairsWithGivenSum(
binarySearchTree.getRoot(),
27
)
);
}

@Test
public void twoNodesWithGivenSumNotPossibleTest() {

BinarySearchTree<Integer> binarySearchTree
= new BinarySearchTree<>();
binarySearchTree.insert(10);
binarySearchTree.insert(8);
binarySearchTree.insert(15);
binarySearchTree.insert(12);
binarySearchTree.insert(6);
binarySearchTree.insert(9);

ArrayList<Pair<Integer, Integer>> result
= new ArrayList<>();
assertEquals(result,
tester.findPairsWithGivenSum(
binarySearchTree.getRoot(),
45
)
);
}

@Test
public void twoNodesWithGivenSumNullTreeTest() {

ArrayList<Pair<Integer,Integer>> result
= new ArrayList<>();

System.out.println(result);

assertEquals(result,
tester.findPairsWithGivenSum(
null,
45
)
);
}
}

```

The complexity of this method to find two nodes with a given sum in a binary search tree is O(n). We are storing nodes in stacks, which will have space complexity of O(n), however, it is less than the previous solutions as it actually making the recursive stack explicit.

Please share if you have any doubts or something is missing or wrong. If you are preparing for an interview, signup to receive a free interview kit.

## Lowest common ancestor (LCA) in BST

Given a binary search tree and two nodes, find the lowest node which is the parent of both given nodes, that is the lowest common ancestor (LCA). For example, in the following tree, LCA of 6 and 1 is node(5), whereas the lowest common ancestor of nodes 17 and 6 would be a node(10).

What is the condition for a node to be LCA of two nodes?  If paths for given nodes diverge from the node, then the node is the lowest common ancestor. While the path is common for both the nodes, nodes are common ancestor but they are not lowest or least. How can we find where paths are diverging?

Paths are diverging when one node is on the left subtree and another node is on the right subtree of the node. The brute force solution would be to find one node and then go up the tree and see at what parent node, other given node falls on the opposite subtree.

Implementation wise, traverse to node 1 and node 2, and store both paths on the stack. Then pop from two stacks till you find the same node on both paths, that node would be the lowest common ancestor. There will be two scans of the tree and additional space complexity to store paths which in the worst case be `O(n)`.

However, the brute force solution does not use the property of a binary search tree. Property is that all the nodes on the left side of a node are smaller and all the nodes on the right side of a node are greater than node. Can we use that property to solve this problem?

Basic idea is to return the node if it is found in any of the subtrees. At any node, search for both given nodes in the left subtree.  If we get a non-empty node returned from the left subtree, there is at least one of the two nodes is on the left subtree.

Again, search in right subtree these two nodes, if a non-empty node is returned from the right subtree, that means at least one of the nodes is on the right subtree.

What does it means if we have a non-empty node on both left and right subtree? It means two nodes are on the left and right subtree, one on each side. It means the root node is the lowest common ancestor.

What if one of the returned nodes is empty? It means both nodes are on one side of the root node, and we should return the upwards the non-empty node returned.

Let’s take an example and see how does it work? Given the below tree, find the lowest common ancestor of node(1) and node(9).

Start with the node(10) and look for the left subtree for both node(1) and node(9). Go down all the way to the node(1), at the time, we return 1 as the node as node.value is equal to one of the nodes.

At node(5), we have got node(1) return from left subtree. We will search for node(1) and node(9) on right subtree. We go all the way to node(6), which is leaf node.

At node(8), the left subtree returns nothing as none of the nodes in the left subtree of node(8). However, the right subtree returns node(9).

As per our algorithm, if either of the subtrees returns a non-empty node, we return the node return from the subtree.

At node(5), we get a non-empty node from the right subtree and we already know, from the left subtree, we got node(1). At this point at node(5), we have both left and right subtree returning non-empty node, hence return the node(5).

Two nodes will be searched on the right subtree of node(10), which will return nothing, hence, final lowest common ancestor will be node(5).

### Implementation

```#include<stdio.h>
#include<stdlib.h>

struct node{
int value;
struct node *left;
struct node *right;
};

typedef struct node Node;

Node * findLCA(Node *root, int val1, int val2)
{
// Base case
if (root == NULL) return NULL;

/* If either val1 or val2 matches with root's key,
report the presence by returning the root
(Note that if a key is the ancestor of other,
then the ancestor key becomes LCA
*/
if (root->key == val1 || root->key == val2)
return root;

// Look for keys in left and right subtrees
Node *left  = findLCA(root->left, val1, val2);
Node *right = findLCA(root->right, val1, val2);

/* If both of the above calls return Non-NULL,
then one key is present in once subtree
and other is present in other,
So this node is the LCA */
if (left && right)  return root;

// Otherwise check if left subtree or right subtree is LCA
return (left != NULL)? left : right;
}

Node * createNode(int value){
Node *newNode =  (Node *)malloc(sizeof(Node));

newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;
}

Node * addNode(Node *node, int value){
if(node == NULL){
return createNode(value);
}
else{
if (node->value > value){
}
else{
}
}
return node;
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;
//Creating a binary tree

printf("\n least common ancestor: %d ",
leastCommonAncestor(root, 15, 25));

return 0;
}
```

Below implementation only works for binary search tree and not for the binary tree as above method works.

```#include<stdio.h>
#include<stdlib.h>

struct node{
int value;
struct node *left;
struct node *right;
};

typedef struct node Node;

int leastCommonAncestor(Node *root, int val1, int val2){

if(!root)
return -1;

if(root->value == val1 || root->value == val2)
return root->value;

/* Case 3: If one value is less and other greater
than the current node
Found the LCS return */
if((root->value > val1 && root->value <= val2) ||
(root->value <= val1 && root->value >val2)){
return root->value;
}
/*Case 2 : If Both values are greater than current node,
look in right subtree */
else if(root->value < val1 && root->value <val2){
return leastCommonAncestor(root->right, val1, val2);
}
/*Case 1 : If Both values are less than current node,
look in left subtree */
else if(root->value > val1 && root->value > val2){
return leastCommonAncestor(root->left, val1, val2);
}
}

Node * createNode(int value){
Node *newNode =  (Node *)malloc(sizeof(Node));

newNode->value = value;
newNode->right= NULL;
newNode->left = NULL;

return newNode;

}

Node * addNode(Node *node, int value){
if(node == NULL){
return createNode(value);
}
else{
if (node->value > value){
}
else{
}
}
return node;
}

/* Driver program for the function written above */
int main(){
Node *root = NULL;
//Creating a binary tree

printf("\n least common ancestor: %d ",
leastCommonAncestor(root, 15, 25));

return 0;
}

```

The worst complexity of the algorithm to find the lowest common ancestor in a binary tree is O(n). Also, keep in mind that recursion is involved. More skewed the tree, more stack frames on the stack and more the chances that stack will overflow.

This problem is solved using on traversal of tree and managing states when returning from recursive calls.

Please share if there is something wrong or missing. If you are willing to contribute and share your knowledge with thousands of learners across the world, please reach out to us at [email protected]

## Kth smallest element in a binary a search tree

Given a binary search tree, find kth smallest element in the binary search tree. For example, 5th smallest element in below binary search tree would be 14, if store the tree in sorted order 5,7,9,10,14,15,19; 14 is the fifth smallest element in that order.

### Kth smallest element in binary search tree: thoughts

As mentioned earlier in a lot of posts like delete a binary tree or mirror a binary tree, first try to find the traversal order required to solve this problem. One hint we already got is that we want all the nodes on BST traversed in sorted order. What kind of traversal gives us a sorted order of nodes? Of course, it is inorder traversal.

So idea is to do an inorder traversal of the binary search tree and store all the nodes in an array. Once traversal is finished, find the kth smallest element in the sorted array.

This approach, however, scans the entire tree and also has space complexity of O(n) because we store all the nodes of tree in an array. Can we avoid scanning the whole tree and storing nodes?

If we keep count of how many nodes are traversed during inorder traversal, we can actually stop traversal as soon as we see k nodes are visited. In this case, we do not store nodes, just a counter.

#### Kth smallest element in binary tree: implementation

```package com.company.BST;

import java.util.Stack;

/**
* Created by sangar on 9.11.18.
*/
public class FindKthSmallestInBST {
private int counter;

public FindKthSmallestInBST(){
counter = 0;
}
public TreeNode findKthSmallest(TreeNode root, int k){
if(root == null) return root;

//Traverse left subtree first
TreeNode left = findKthSmallest(root.getLeft(),k);
//If we found kth node on left subtree
if(left != null) return left;
//If k becomes zero, that means we have traversed k nodes.
if(++counter == k) return root;

return findKthSmallest(root.getRight(),k);
}
}
```

Test cases

```package test;

import com.company.BST.BinarySearchTree;
import com.company.BST.FindKthSmallestInBST;
import com.company.BST.TreeNode;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Created by sangar on 23.9.18.
*/
public class KthSmallestElementTest {

FindKthSmallestInBST tester = new FindKthSmallestInBST();

@Test
public void kthSmallestElementTest() {

BinarySearchTree&lt;Integer&gt; binarySearchTree =
new BinarySearchTree&lt;&gt;();
binarySearchTree.insert(10);
binarySearchTree.insert(8);
binarySearchTree.insert(15);
binarySearchTree.insert(12);
binarySearchTree.insert(6);
binarySearchTree.insert(9);

TreeNode kthNode =
tester.findKthSmallest(binarySearchTree.getRoot(),1);
assertEquals(6, kthNode.getValue());
}

@Test
public void kthSmallestElementOnRightSubtreeTest() {

BinarySearchTree&lt;Integer&gt; binarySearchTree =
new BinarySearchTree&lt;&gt;();
binarySearchTree.insert(10);
binarySearchTree.insert(8);
binarySearchTree.insert(15);
binarySearchTree.insert(12);
binarySearchTree.insert(6);
binarySearchTree.insert(9);

TreeNode kthNode =
tester.findKthSmallest(binarySearchTree.getRoot(),5);
assertEquals(12, kthNode.getValue());
}

@Test
public void kthSmallestElementAbsentSubtreeTest() {

BinarySearchTree&lt;Integer&gt; binarySearchTree =
new BinarySearchTree&lt;&gt;();
binarySearchTree.insert(10);
binarySearchTree.insert(8);
binarySearchTree.insert(15);
binarySearchTree.insert(12);
binarySearchTree.insert(6);
binarySearchTree.insert(9);

TreeNode kthNode =
tester.findKthSmallest(binarySearchTree.getRoot(),10);
assertEquals(null, kthNode);
}

@Test
public void kthSmallestElementNulltreeTest() {

BinarySearchTree&lt;Integer&gt; binarySearchTree =
new BinarySearchTree&lt;&gt;();
binarySearchTree.insert(10);
binarySearchTree.insert(8);
binarySearchTree.insert(15);
binarySearchTree.insert(12);
binarySearchTree.insert(6);
binarySearchTree.insert(9);

TreeNode kthNode = tester.findKthSmallest(null,10);
assertEquals(null, kthNode);
}
}
```

The complexity of this algorithm to find kth smallest element is `O(k)` as we traverse only k nodes on binary search tree.

There is hidden space complexity here. Recursive function requires call stack memory, which is limited to Operation System default. More deep you go in recursion, more space we use on stack. If tree is completely skewed, there are more chances of stack overflow. Also recursive function is very difficult to debug in production environments. Below is the non-recursive solution for the same problem.

#### Non-recursive way to find kth smallest element in BST

```public int kthSmallest(TreeNode root, int k) {
Stack&lt;TreeNode&gt; s = new Stack&lt;TreeNode&gt;();

TreeNode current = root;
int result = 0;

while(!s.isEmpty() || current!=null){
if(current!=null){
s.push(current);
current = current.getLeft();
}else{
TreeNode  temp = s.pop();
k--;
if(k==0)
result = (int)temp.getValue();
current = temp.getRight();
}
}

return result;
}
```

Please share if there is something wrong or missing. If you are preparing for an interview and need help, please reach out to us at [email protected]

## Find Kth smallest element in array

Given an array of integers which is non sorted, find kth smallest element in that array. For example: if input array is A = [3,5,1,2,6,9,7], 4th smallest element in array A is 5, because if you sort the array A, it looks like A = [1,2,3,5,6,7,9] and now you can easily see that 4th element is 5.

This problem is commonly asked in Microsoft and Amazon interviews as it has multiple layers and there are some many things that can be tested with this one problem.

## Kth smallest element : Line of thought

First of all, in any interview, try to come up with brute force solution. Brute force solution to find Kth smallest element in array of integers would be to sort the array and return A[k-1] element (K-1 as array is zero base indexed).

What is the complexity of brute force solution? It’s `O(n2)`? Well, we have sort algorithms like merge sort and heap sort which work in O(nlogn) complexity.

The problem with both searches is that they use additional space. Quick sort is another sorting algorithm. It has problem that it’s worst-case complexity will be `O(n2)`, which happens when input is completely sorted.
In our case, the input is given as unsorted already, so we can expect that quicksort will function with `O(n log n)` complexity which is its average-case complexity. Advantage of using quicksort is that there is no additional space complexity.

Optimising quick sort

Let’s see how quicksort works and see if we can optimize solution further?
Idea behind quicksort is to find the correct place for the selected pivot. Once the pivot is at the correct position, all the elements on the left side of the pivot are smaller and on the right side of the pivot are greater than the pivot. This step is partitioning.

If after partitioning, pivot is at position j, can we say that pivot is actually jth smallest element of the array? What if j is equal to k? Well problem solved, we found the kth smallest element.

If j is less than k, left subarray is less than k, we need to include more elements from right subarray, therefore kth smallest element is in right subarray somewhere. We have already found j smallest elements, all we need to find is k-j elements from right subarray.

What if j is greater than k? In this case, we have to drop some elements from left subarray, so our search space would be left subarray after partition.

Theoretically, this algorithm still has the complexity of `O(n log n)`, but practically, you do not need to sort the entire array before you find k smallest elements.

If you are preparing for a technical interview and need personal coaching along with mock interviews, book a free demo session with us

### Algorithm to find Kth smallest element in array

1. Select a pivot and partition the array with pivot at correct position j
2. If position of pivot, j, is equal to k, return A[j].
3. If j is less than k, discard array from start to j, and look for (k-j)th smallest element in right sub array, go to step 1.
4. If j is greater than k, discard array from j to end and look for kth element in left subarray, go to step 1

Let’s take an example and see if this algorithm works? A =  [4, 2, 1, 7, 5, 3, 8, 10, 9, 6 ], and we have to find fifth smallest element in array A.

Start with pivot as first index of array, so pivot = 0, partition the array into two parts around pivot such that all elements on left side of pivot element, i.e. A[pivot] are smaller and all elements on right side are greater than A[pivot].

Start with pivot as first index of array, so pivot = 0, partition the array into two parts around pivot such that all elements on left side of pivot element, i.e. A[pivot] are smaller and all elements on right side are greater than A[pivot].

In our example, array A will look like below after pivot has found it’s the correct position.

If pivot == k-1 (array is represented as zero base index), then A[pivot] is kth smallest element. Since pivot (3) is less than k-1 (4), look for kth smallest element on right side of the pivot.

k remains as it is as opposed to k-j mentioned in algorithm as pivot is given w.r.t entire array and not w.r.t subarray.

In second iteration, pivot = 4 (index and not element). After second execution of quick sort array A will be like

pivot(4) which is equal to k-1(5-1). 5th smallest element in array A is 5.

### Implementation

```package com.company;

/**
* Created by sangar on 30.9.18.
*/
public class KthSmallest {
private void swap(int[] a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private int partition(int[] a, int start, int end){
int pivot = a[start];
int i  = start+1;
int j  = end;

while(i <= j){
while(a[i] < pivot) i++;
while(a[j] > pivot) j--;

if(i < j) {
swap(a, i, j);
}
}
swap(a, start, j);
return j;
}

public int findKthSmallestElement(int a[], int start,
int end, int k){
if(start <= end){
int p = partition(a, start, end);
if(p == k-1){
return a[p];
}
if(p > k-1)
return findKthSmallestElement(a, start, p, k);
if(p < k-1)
return findKthSmallestElement(a, p+1, end, k);
}
return -1;
}
}
```

Test cases

```package test;

import com.company.KthSmallest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* Created by sangar on 28.8.18.
*/
public class KthSmallestTest {

KthSmallest tester = new KthSmallest();
private int[] a = {4, 2, 1, 7, 5, 3, 8, 10, 9};
@Test
public void kthSmallest() {
assertEquals(7, tester.findKthSmallestElement(a,0,8,6));
}

@Test
public void firstSmallest() {
assertEquals(1, tester.findKthSmallestElement(a,0,8,1));
}

@Test
public void lastSmallest() {
assertEquals(10, tester.findKthSmallestElement(a,0,8,9));
}

@Test
public void kGreaterThanSize() {
assertEquals(-1, tester.findKthSmallestElement(a,0,8,15));
}
@Test
public void emptyArray() {
int[] a = {};
assertEquals(-1, tester.findKthSmallestElement(a,0,0,1));
}

@Test
public void nullArray() {
assertEquals(-1, tester.findKthSmallestElement(null,0,0,1));
}
}
```

Complexity of using quicksort algorithm to find the kth smallest element in the array of integers is still O(n logn).

### Kth smallest element using heaps

Before going into details of this problem, I strongly recommend reading heap fundamentals.

Imagine a case where there are a billion integers in the array and you have to find 5 smallest elements from that array. The complexity of O(n log n) is too costly for that use case. Above algorithm using quicksort does not take into consideration disparity between k and n.

We want top k elements, how about we chose those k elements randomly, call it `set A` and then go through all other n-k elements, call it `set B`, check if element from set B (n-k elements) can displace element in set A (k elements)?

What will be the condition for an element from set B to replace an element in set A? Well, if the new element is less than maximum in set A than the maximum in set A cannot be in the set of k smallest elements right?  Maximum element in set A would be replaced by the new element from set B.

Now, the problem is how to quickly find the maximum out of set A. Heap is the best data structure there. What kind of heap: min heap or max heap? Max heap as it store the maximum of the set at the root of it.

Let’s defined concrete steps to find k smallest elements using a max heap.

1. Create a max heap of size k from first k elements of array.
2. Scan all elements in array one by one.
1.  If current element is less than max on heap, add current element to heap and heapify.
2. If not, then go to next element.
3. At the end, max heap will contain k smallest elements of array and root will be kth smallest element.

Let’s take an example and see if this algorithm works? The input array is shown below and we have to find the 6th smallest element in this array.

Step 1 : Create a max heap with first 6 elements of array.

Step 2: Take the next element from set B and check if it is less than the root of max heap. In this case, yes it is. Remove the root and insert the new element into max heap.

Step 2: It continues to 10, nothing happens as the new element is greater than the root of max heap. Same for 9.  At 6, again the root of max heap is greater than 6. Remove the root and add 6 to max heap.

Array scan is finished, so just return the root of the max heap, 6 which is the sixth smallest element in given array.

### Implementation

```	public int findKthSmallestElementUsingHeap(int a[], int k){
//https://stackoverflow.com/questions/11003155/change-priorityqueue-to-max-priorityqueue

PriorityQueue<Integer>  maxHeap =
new PriorityQueue<>(k, Collections.reverseOrder());

if(a == null || k > a.length) return -1;
//Create max with first k elements
for(int i=0; i<k; i++){
}

/*Keep updating max heap based on a new element
If new element is less than root,
remove root and add new element
*/

for(int i=k; i<a.length; i++){
if(maxHeap.peek() > a[i]){
maxHeap.remove();
}
}
return maxHeap.peek();
}
```

Can you calculate the complexity of above algorithm? `heapify()` has complexity of log(k) with k elements on heap. In worst case, we have to do `heapify()` for all elements in array, which is n, so overall complexity of algorithm becomes O(nlogk). Also, there is additional space complexity of O(k) to store heap.
When is very small as compared to n, this algorithm again depends on the size of array.

We want k smallest elements, if we pick first k elements from a min heap, will it solve the problem? I think so. Create a min heap of n elements in place from the given array, and then pick first k elements.
Creation of heap has complexity of O(n), do more reading on it. All we need to do is delete k times from this heap, each time there will be `heapify()`. It will have complexity of O(log n) for n element heap. So, overall complexity would be O(n + klogn).

Depending on what you want to optimize, select correct method to find kth smallest element in array.

Please share if there is something wrong or missing. If you are interested in taking coaching sessions from our experienced teachers, please reach out to us at [email protected]