Open In App

Find the node with minimum value in a Binary Search Tree

Last Updated : 28 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Write a function to find the node with minimum value in a Binary Search Tree.

Example: 

Input: 
 

first example BST

Output: 8

Input: 
 

second example BST

Output: 10 

Recommended Practice

Brute Force Approach: To solve the problem follow the below idea:

The in-order traversal of a binary search tree always returns the value of nodes in sorted order. So the 1st value in the sorted vector will be the minimum value  which is the answer.

Below is the implementation of the above approach:

C++14




// C++ program to find minimum value node in binary search
// Tree.
 
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
/* A binary tree node has data, pointer to left child
   and a pointer to right child */
struct node {
    int data;
    struct node* left;
    struct node* right;
};
 
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
struct node* newNode(int data)
{
    struct node* node
        = (struct node*)malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return (node);
}
 
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
struct node* insert(struct node* node, int data)
{
    /* 1. If the tree is empty, return a new,
        single node */
    if (node == NULL)
        return (newNode(data));
    else {
        /* 2. Otherwise, recur down the tree */
        if (data <= node->data)
            node->left = insert(node->left, data);
        else
            node->right = insert(node->right, data);
 
        /* return the (unchanged) node pointer */
        return node;
    }
}
 
/* Given a non-empty binary search tree,
inorder traversal for the tree is stored in
the vector sortedInorder.
Inorder is LEFT,ROOT,RIGHT*/
void inorder(struct node* node, vector<int>& sortedInorder)
{
    if (node == NULL)
        return;
    /* first recur on left child */
    inorder(node->left, sortedInorder);
 
    /* then insert the data of node */
    sortedInorder.push_back(node->data);
 
    /* now recur on right child */
    inorder(node->right, sortedInorder);
}
 
/* Driver code*/
int main()
{
    struct node* root = NULL;
    root = insert(root, 4);
    insert(root, 2);
    insert(root, 1);
    insert(root, 3);
    insert(root, 6);
    insert(root, 4);
    insert(root, 5);
 
    vector<int> sortedInorder;
    inorder(
        root,
        sortedInorder); // calling the recursive function
    // values of all nodes will appear in sorted order in
    // the vector sortedInorder
    // Function call
    printf("\n Minimum value in BST is %d",
           sortedInorder[0]);
    getchar();
    return 0;
}


Java




import java.util.*;
 
/* A binary tree node has data, pointer to left child
and a pointer to right child */
class Node {
    int data;
    Node left, right;
 
    /* Helper function that allocates a new node
    with the given data and NULL left and right
    pointers. */
    public Node(int data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
 
class Main {
 
    /* Give a binary search tree and a number,
    inserts a new node with the given number in
    the correct place in the tree. Returns the new
    root pointer which the caller should then use
    (the standard trick to avoid using reference
    parameters). */
    public static Node insert(Node node, int data)
    {
 
        /* 1. If the tree is empty, return a new,
                single node */
        if (node == null) {
            return new Node(data);
        }
        else {
 
            /* 2. Otherwise, recur down the tree */
            if (data <= node.data) {
                node.left = insert(node.left, data);
            }
            else {
                node.right = insert(node.right, data);
            }
 
            /* return the (unchanged) node pointer */
            return node;
        }
    }
 
    /* Given a non-empty binary search tree,
    inorder traversal for the tree is stored in
    the vector sortedInorder.
    Inorder is LEFT,ROOT,RIGHT*/
    public static void inorder(Node node,
                               List<Integer> sortedInorder)
    {
        if (node == null) {
            return;
        }
        /* first recur on left child */
        inorder(node.left, sortedInorder);
 
        /* then insert the data of node */
        sortedInorder.add(node.data);
 
        /* now recur on right child */
        inorder(node.right, sortedInorder);
    }
 
    public static void main(String[] args)
    {
        Node root = null;
        root = insert(root, 4);
        insert(root, 2);
        insert(root, 1);
        insert(root, 3);
        insert(root, 6);
        insert(root, 4);
        insert(root, 5);
 
        List<Integer> sortedInorder
            = new ArrayList<Integer>();
 
        inorder(root, sortedInorder); // calling the
                                      // recursive function
        // values of all nodes will appear in sorted order
        // in the vector sortedInorder
        // Function call
        System.out.printf("\n Minimum value in BST is %d",
                          sortedInorder.get(0));
    }
}


Python3




from typing import List
 
 
class Node:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
 
# Give a binary search tree and a number,
# inserts a new node with the given number
# in the correct place in the tree. Returns
# the new root pointer which the caller should then use
# (the standard trick to avoid using reference parameters).
 
 
def insert(node: Node, data: int) -> Node:
 
    # If the tree is empty, return a new, single node
    if not node:
        return Node(data)
 
    # Otherwise, recur down the tree
    if data <= node.data:
        node.left = insert(node.left, data)
    else:
        node.right = insert(node.right, data)
 
    # Return the (unchanged) node pointer
    return node
 
# Given a non-empty binary search tree, inorder traversal for
# the tree is stored in the list sorted_inorder. Inorder is LEFT,ROOT,RIGHT.
 
 
def inorder(node: Node, sorted_inorder: List[int]) -> None:
 
    if not node:
        return
 
    # First recur on left child
    inorder(node.left, sorted_inorder)
 
    # Then insert the data of node
    sorted_inorder.append(node.data)
 
    # Now recur on right child
    inorder(node.right, sorted_inorder)
 
 
if __name__ == '__main__':
    root = None
    root = insert(root, 4)
    insert(root, 2)
    insert(root, 1)
    insert(root, 3)
    insert(root, 6)
    insert(root, 4)
    insert(root, 5)
 
    sorted_inorder = []
    inorder(root, sorted_inorder)  # calling the recursive function
 
    # Values of all nodes will appear in sorted order in the list sorted_inorder
    print(f"Minimum value in BST is {sorted_inorder[0]}")


C#




using System;
using System.Collections.Generic;
 
// A binary tree node has data, pointer to left child,
// and a pointer to right child
class Node {
    public int data;
    public Node left, right;
 
    // Helper function that allocates a new node
    // with the given data and NULL left and right
    // pointers.
    public Node(int data)
    {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
 
class MainClass {
    // Give a binary search tree and a number,
    // inserts a new node with the given number in
    // the correct place in the tree. Returns the new
    // root pointer which the caller should then use
    // (the standard trick to avoid using reference
    // parameters).
    public static Node insert(Node node, int data)
    {
        // 1. If the tree is empty, return a new,
        // single node
        if (node == null) {
            return new Node(data);
        }
        else {
            // 2. Otherwise, recur down the tree
            if (data <= node.data) {
                node.left = insert(node.left, data);
            }
            else {
                node.right = insert(node.right, data);
            }
 
            // return the (unchanged) node pointer
            return node;
        }
    }
 
    // Given a non-empty binary search tree,
    // inorder traversal for the tree is stored in
    // the List sortedInorder.
    // Inorder is LEFT,ROOT,RIGHT
    public static void inorder(Node node,
                               List<int> sortedInorder)
    {
        if (node == null) {
            return;
        }
 
        // first recur on left child
        inorder(node.left, sortedInorder);
 
        // then insert the data of node
        sortedInorder.Add(node.data);
 
        // now recur on right child
        inorder(node.right, sortedInorder);
    }
 
    public static void Main(string[] args)
    {
        Node root = null;
        root = insert(root, 4);
        insert(root, 2);
        insert(root, 1);
        insert(root, 3);
        insert(root, 6);
        insert(root, 4);
        insert(root, 5);
 
        List<int> sortedInorder = new List<int>();
 
        inorder(root, sortedInorder); // calling the
                                      // recursive function
 
        // values of all nodes will appear in sorted order
        // in the list sortedInorder
        // Function call
        Console.WriteLine("\n Minimum value in BST is {0}",
                          sortedInorder[0]);
    }
}


Javascript




// A binary tree node has data, pointer to left child
// and a pointer to right child
class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
 
// Helper function that allocates a new node
// with the given data and NULL left and right
// pointers.
function newNode(data) {
    return new Node(data);
}
 
// Give a binary search tree and a number,
// inserts a new node with the given number in
// the correct place in the tree. Returns the new
// root pointer which the caller should then use
// (the standard trick to avoid using reference
// parameters).
function insert(node, data) {
    // 1. If the tree is empty, return a new,
    // single node
    if (node == null) return newNode(data);
    else {
        // 2. Otherwise, recur down the tree
        if (data <= node.data) node.left = insert(node.left, data);
        else node.right = insert(node.right, data);
 
        // return the (unchanged) node pointer
        return node;
    }
}
 
// Given a non-empty binary search tree,
// inorder traversal for the tree is stored in
// the vector sortedInorder.
// Inorder is LEFT,ROOT,RIGHT
function inorder(node, sortedInorder) {
    if (node == null) return;
    // first recur on left child
    inorder(node.left, sortedInorder);
 
    // then insert the data of node
    sortedInorder.push(node.data);
 
    // now recur on right child
    inorder(node.right, sortedInorder);
}
 
// Driver code
let root = null;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 4);
insert(root, 5);
 
let sortedInorder = [];
inorder(root, sortedInorder);
console.log("Minimum value in BST is " + sortedInorder[0]);


Output

 Minimum value in BST is 1


Time Complexity: O(n) where n is the number of nodes.
Auxiliary Space: O(n) (for recursive stack space + vector used additionally)

Efficient Approach: To solve the problem follow the below idea:

This is quite simple. Just traverse the node from root to left recursively until left is NULL. The node whose left is NULL is the node with minimum value

Below is the implementation of the above approach:

C++




// C++ program to find minimum value node in binary search
// Tree.
#include <bits/stdc++.h>
using namespace std;
 
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node {
    int data;
    struct node* left;
    struct node* right;
};
 
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
struct node* newNode(int data)
{
    struct node* node
        = (struct node*)malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return (node);
}
 
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
struct node* insert(struct node* node, int data)
{
    /* 1. If the tree is empty, return a new,
        single node */
    if (node == NULL)
        return (newNode(data));
    else {
        /* 2. Otherwise, recur down the tree */
        if (data <= node->data)
            node->left = insert(node->left, data);
        else
            node->right = insert(node->right, data);
 
        /* return the (unchanged) node pointer */
        return node;
    }
}
 
/* Given a non-empty binary search tree,
return the minimum data value found in that
tree. Note that the entire tree does not need
to be searched. */
int minValue(struct node* node)
{
    struct node* current = node;
 
    /* loop down to find the leftmost leaf */
    while (current->left != NULL) {
        current = current->left;
    }
    return (current->data);
}
 
/* Driver Code*/
int main()
{
    struct node* root = NULL;
    root = insert(root, 4);
    insert(root, 2);
    insert(root, 1);
    insert(root, 3);
    insert(root, 6);
    insert(root, 5);
 
      // Function call
    cout << "\n Minimum value in BST is " << minValue(root);
    getchar();
    return 0;
}
 
// This code is contributed by Mukul Singh.


C




// C program to find minimum value node in binary search
// Tree.
 
#include <stdio.h>
#include <stdlib.h>
 
/* A binary tree node has data, pointer to left child
   and a pointer to right child */
struct node {
    int data;
    struct node* left;
    struct node* right;
};
 
/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
struct node* newNode(int data)
{
    struct node* node
        = (struct node*)malloc(sizeof(struct node));
    node->data = data;
    node->left = NULL;
    node->right = NULL;
 
    return (node);
}
 
/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
struct node* insert(struct node* node, int data)
{
    /* 1. If the tree is empty, return a new,
        single node */
    if (node == NULL)
        return (newNode(data));
    else {
        /* 2. Otherwise, recur down the tree */
        if (data <= node->data)
            node->left = insert(node->left, data);
        else
            node->right = insert(node->right, data);
 
        /* return the (unchanged) node pointer */
        return node;
    }
}
 
/* Given a non-empty binary search tree,
return the minimum data value found in that
tree. Note that the entire tree does not need
to be searched. */
int minValue(struct node* node)
{
    struct node* current = node;
 
    /* loop down to find the leftmost leaf */
    while (current->left != NULL) {
        current = current->left;
    }
    return (current->data);
}
 
/* Driver code*/
int main()
{
    struct node* root = NULL;
    root = insert(root, 4);
    insert(root, 2);
    insert(root, 1);
    insert(root, 3);
    insert(root, 6);
    insert(root, 5);
 
      // Function call
    printf("\n Minimum value in BST is %d", minValue(root));
    getchar();
    return 0;
}


Java




// Java program to find minimum value node in Binary Search
// Tree
 
// A binary tree node
class Node {
 
    int data;
    Node left, right;
 
    Node(int d)
    {
        data = d;
        left = right = null;
    }
}
 
class BinaryTree {
 
    static Node head;
 
    /* Given a binary search tree and a number,
     inserts a new node with the given number in
     the correct place in the tree. Returns the new
     root pointer which the caller should then use
     (the standard trick to avoid using reference
     parameters). */
    Node insert(Node node, int data)
    {
 
        /* 1. If the tree is empty, return a new,
         single node */
        if (node == null) {
            return (new Node(data));
        }
        else {
 
            /* 2. Otherwise, recur down the tree */
            if (data <= node.data) {
                node.left = insert(node.left, data);
            }
            else {
                node.right = insert(node.right, data);
            }
 
            /* return the (unchanged) node pointer */
            return node;
        }
    }
 
    /* Given a non-empty binary search tree,
     return the minimum data value found in that
     tree. Note that the entire tree does not need
     to be searched. */
    int minvalue(Node node)
    {
        Node current = node;
 
        /* loop down to find the leftmost leaf */
        while (current.left != null) {
            current = current.left;
        }
        return (current.data);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        BinaryTree tree = new BinaryTree();
        Node root = null;
        root = tree.insert(root, 4);
        tree.insert(root, 2);
        tree.insert(root, 1);
        tree.insert(root, 3);
        tree.insert(root, 6);
        tree.insert(root, 5);
 
          // Function call
        System.out.println("Minimum value of BST is "
                           + tree.minvalue(root));
    }
}
 
// This code is contributed by Mayank Jaiswal


Python3




# Python3 program to find the node with minimum value in bst
 
# A binary tree node
 
 
class Node:
 
    # Constructor to create a new node
    def __init__(self, key):
        self.data = key
        self.left = None
        self.right = None
 
 
""" Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). """
 
 
def insert(node, data):
 
    # 1. If the tree is empty, return a new,
    # single node
    if node is None:
        return (Node(data))
 
    else:
        # 2. Otherwise, recur down the tree
        if data <= node.data:
            node.left = insert(node.left, data)
        else:
            node.right = insert(node.right, data)
 
        # Return the (unchanged) node pointer
        return node
 
 
""" Given a non-empty binary search tree, 
return the minimum data value found in that
tree. Note that the entire tree does not need
to be searched. """
 
 
def minValue(node):
    current = node
 
    # loop down to find the leftmost leaf
    while(current.left is not None):
        current = current.left
 
    return current.data
 
 
# Driver code
if __name__ == '__main__':
  root = None
  root = insert(root, 4)
  insert(root, 2)
  insert(root, 1)
  insert(root, 3)
  insert(root, 6)
  insert(root, 5)
 
  # Function call
  print("\nMinimum value in BST is %d" % (minValue(root)))
 
# This code is contributed by Nikhil Kumar Singh(nickzuck_007)


C#




// C# program to find minimum value node in Binary Search
// Tree
 
using System;
 
// C# program to find minimum value node in Binary Search
// Tree
 
// A binary tree node
public class Node {
 
    public int data;
    public Node left, right;
 
    public Node(int d)
    {
        data = d;
        left = right = null;
    }
}
 
public class BinaryTree {
 
    public static Node head;
 
    /* Given a binary search tree and a number,
     inserts a new node with the given number in
     the correct place in the tree. Returns the new
     root pointer which the caller should then use
     (the standard trick to avoid using reference
     parameters). */
    public virtual Node insert(Node node, int data)
    {
 
        /* 1. If the tree is empty, return a new,
         single node */
        if (node == null) {
            return (new Node(data));
        }
        else {
 
            /* 2. Otherwise, recur down the tree */
            if (data <= node.data) {
                node.left = insert(node.left, data);
            }
            else {
                node.right = insert(node.right, data);
            }
 
            /* return the (unchanged) node pointer */
            return node;
        }
    }
 
    /* Given a non-empty binary search tree,
     return the minimum data value found in that
     tree. Note that the entire tree does not need
     to be searched. */
    public virtual int minvalue(Node node)
    {
        Node current = node;
 
        /* loop down to find the leftmost leaf */
        while (current.left != null) {
            current = current.left;
        }
        return (current.data);
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        BinaryTree tree = new BinaryTree();
        Node root = null;
        root = tree.insert(root, 4);
        tree.insert(root, 2);
        tree.insert(root, 1);
        tree.insert(root, 3);
        tree.insert(root, 6);
        tree.insert(root, 5);
 
          // Function call
        Console.WriteLine("Minimum value of BST is "
                          + tree.minvalue(root));
    }
}
 
// This code is contributed by Shrikant13


Javascript




<script>
 
    // JavaScript program to find minimum
    // value node in Binary Search Tree
     
    class Node
    {
        constructor(data) {
           this.left = null;
           this.right = null;
           this.data = data;
        }
    }
     
    let head;
       
    /* Given a binary search tree and a number,
     inserts a new node with the given number in
     the correct place in the tree. Returns the new
     root pointer which the caller should then use
     (the standard trick to avoid using reference
     parameters). */
    function insert(node, data) {
           
        /* 1. If the tree is empty, return a new,    
         single node */
        if (node == null) {
            return (new Node(data));
        } else {
               
            /* 2. Otherwise, recur down the tree */
            if (data <= node.data) {
                node.left = insert(node.left, data);
            } else {
                node.right = insert(node.right, data);
            }
   
            /* return the (unchanged) node pointer */
            return node;
        }
    }
   
    /* Given a non-empty binary search tree, 
     return the minimum data value found in that
     tree. Note that the entire tree does not need
     to be searched. */
    function minvalue(node) {
        if (node === null) return null;
        let current = node;
   
        /* loop down to find the leftmost leaf */
        while (current.left != null) {
            current = current.left;
        }
        return (current.data);
    }
     
    let root = null;
    root = insert(root, 4);
    insert(root, 2);
    insert(root, 1);
    insert(root, 3);
    insert(root, 6);
    insert(root, 5);
 
    document.write("Minimum value in BST is " + minvalue(root));
 
</script>


PHP




<?php
// PHP program to find the node with
// minimum value in bst
 
// create a binary tree
class node
{
    private $node, $left, $right;
    function __construct($node)
    {
        $this->node = $node;
        $left = $right = NULL;
    }
 
    // set the left node in tree
    function set_left($left)
    {
        $this->left = $left;
    }
 
    // set the right node in tree
    function set_right($right)
    {
        $this->right = $right;
    }
 
    // get left node
    function get_left()
    {
        return $this->left;
    }
 
    // get right node
    function get_right()
    {
        return $this->right;
    }
 
    // get value of current node
    function get_node()
    {
        return $this->node;
    }
     
}
 
// Find the node with minimum value
// in a Binary Search Tree
function get_minimum_value($node)
{
    /*travel till last left node to
      get the minimum value*/
    while ($node->get_left() != NULL)
    {
        $node = $node->get_left();
    }
    return $node->get_node();
}
 
// code to creating a tree
$node = new node(4);
$lnode = new node(2);
$lnode->set_left(new node(1));
$lnode->set_right(new node(3));
$rnode = new node(6);
$rnode->set_left(new node(5));
$node->set_left($lnode);
$node->set_right($rnode);
 
$minimum_value = get_minimum_value($node);
echo 'Minimum value of BST is '.
                 $minimum_value;
 
// This code is contributed
// by Deepika Pathak
?>


Output

 Minimum value in BST is 1


Time Complexity: O(h) where h is the height of Binary Search Tree
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Find the node with minimum value in a Binary Search Tree using recursion
Given a Binary Search Tree, the task is to find the node with minimum value. Examples: Input: Output: 4 Approach: Just traverse the node from root to left recursively until left is NULL. The node whose left is NULL is the node with the minimum value. Below is the implementation of the above approach: C/C++ Code // C++ implementation of the approach
7 min read
Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
In this article, we will discuss the complexity of different operations in binary trees including BST and AVL trees. Before understanding this article, you should have a basic idea about Binary Tree, Binary Search Tree, and AVL Tree. The main operations in a binary tree are: search, insert and delete. We will see the worst-case time complexity of t
4 min read
Minimum swap required to convert binary tree to binary search tree
Given the array representation of Complete Binary Tree i.e, if index i is the parent, index 2*i + 1 is the left child and index 2*i + 2 is the right child. The task is to find the minimum number of swap required to convert it into Binary Search Tree. Examples: Input : arr[] = { 5, 6, 7, 8, 9, 10, 11 } Output : 3 Binary tree of the given array: Swap
8 min read
Find the node with maximum value in a Binary Search Tree using recursion
Given a Binary Search Tree, the task is to find the node with maximum value. Examples: Input: Output: 22 Approach: Just traverse the node from root to right recursively until right is NULL. The node whose right is NULL is the node with the maximum value. Below is the implementation of the above approach: C/C++ Code // C++ implementation of the appr
7 min read
Find the node with maximum value in a Binary Search Tree
Given a Binary Search Tree, the task is to find the node with the maximum value in a BST. For the above tree, we start with 20, then we move right to 22. We keep on moving to the right until we see NULL. Since the right of 22 is NULL, 22 is the node with the maximum value. Approach: This is quite simple. Just traverse the node from root to right re
15 min read
Binary Search Tree vs Ternary Search Tree
For effective searching, insertion, and deletion of items, two types of search trees are used: the binary search tree (BST) and the ternary search tree (TST). Although the two trees share a similar structure, they differ in some significant ways. FeatureBinary Search Tree (BST)Ternary Search Tree (TST)NodeHere, each node has at most two children. H
3 min read
Find the minimum Sub-tree with target sum in a Binary search tree
Given a binary tree and a target, find the number of nodes in the minimum sub-tree with the given sum equal to the target which is also a binary search tree. Examples: Input: 13 / \ 5 23 / \ / \ N 17 N N / 16Target: 38Output: 3Explanation: 5, 17, 16 is the smallest subtree with length 3. Input: 7 / \ N 23 / \ 10 23 / \ / \ N 17 N NTarget: 73Output:
9 min read
Binary Tree to Binary Search Tree Conversion using STL set
Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of the Binary Tree.This solution will use Sets of C++ STL instead of array-based solution. Examples: Example 1 Input: 10 / \ 2 7 / \ 8 4 Output: 8 / \ 4 10 / \ 2 7 Example 2 Input: 10 / \ 30 15 / \ 20 5 Output: 15 / \
12 min read
Difference between Binary Tree and Binary Search Tree
Binary Tree Data Structure:A tree whose elements have at most 2 children is called a binary tree. Since each element in a binary tree can have only 2 children, we typically name them the left and right children.  Binary Search Tree Data Structure (BST):A binary search tree is a hierarchical data structure where each node has at most two children, w
2 min read
Binary Tree to Binary Search Tree Conversion
Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of Binary Tree. Examples Example 1Input: 10 / \ 2 7 / \ 8 4Output: 8 / \ 4 10 / \ 2 7Example 2Input: 10 / \ 30 15 / \ 20 5Output: 15 / \ 10 20 / \ 5 30Solution: Following is a 3 step solution for converting Binary tre
15+ min read