Maximum height of the binary search tree created from the given array
Last Updated :
12 Jul, 2025
Given an array arr[] of N integers, the task is to make two binary search trees. One while traversing from the left side of the array and another while traversing from the right and find which tree has a greater height.
Examples:
Input: arr[] = {2, 1, 3, 4}
Output: 0
BST starting from first index BST starting from last index
2 4
/ \ /
1 3 3
\ /
4 1
\
2
Input: arr[] = {1, 2, 6, 3, 5}
Output: 1
Prerequisites: Inserting a node in a Binary Search tree and Finding the height of a binary tree.
Approach:
- Create a binary search tree while inserting the nodes starting from the first element of the array till the last and find the height of this created tree say leftHeight.
- Create another binary search tree while inserting the nodes starting from the last element of the array till the first and find the height of this created tree say rightHeight.
- Print the maximum of these calculated heights i.e. max(leftHeight, rightHeight)
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
struct node {
int key;
struct node *left, *right;
};
// A utility function to create a new BST node
struct node* newNode(int item)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
/* A utility function to insert a new node with given key in BST */
struct node* insert(struct node* node, int key)
{
/* If the tree is empty, return a new node */
if (node == NULL)
return newNode(key);
/* Otherwise, recur down the tree */
if (key < node->key)
node->left = insert(node->left, key);
else if (key > node->key)
node->right = insert(node->right, key);
/* return the (unchanged) node pointer */
return node;
}
/* Compute the "maxDepth" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node.*/
int maxDepth(node* node)
{
if (node == NULL)
return 0;
else {
/* compute the depth of each subtree */
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
/* use the larger one */
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
// Function to return the maximum
// heights among the BSTs
int maxHeight(int a[], int n)
{
// Create a BST starting from
// the first element
struct node* rootA = NULL;
rootA = insert(rootA, a[0]);
for (int i = 1; i < n; i++)
insert(rootA, a[i]);
// Create another BST starting
// from the last element
struct node* rootB = NULL;
rootB = insert(rootB, a[n - 1]);
for (int i = n - 2; i >= 0; i--)
insert(rootB, a[i]);
// Find the heights of both the trees
int A = maxDepth(rootA) - 1;
int B = maxDepth(rootB) - 1;
return max(A, B);
}
// Driver code
int main()
{
int a[] = { 2, 1, 3, 4 };
int n = sizeof(a) / sizeof(a[0]);
cout << maxHeight(a, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static class node
{
int key;
node left, right;
};
// A utility function to create a new BST node
static node newNode(int item)
{
node temp = new node();
temp.key = item;
temp.left = temp.right = null;
return temp;
}
/* A utility function to insert
a new node with given key in BST */
static node insert(node node, int key)
{
/* If the tree is empty,
return a new node */
if (node == null)
return newNode(key);
/* Otherwise, recur down the tree */
if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
/* return the (unchanged) node pointer */
return node;
}
/* Compute the "maxDepth" of a tree --
the number of nodes along the longest path
from the root node down to the farthest leaf node.*/
static int maxDepth(node node)
{
if (node == null)
return 0;
else
{
/* compute the depth of each subtree */
int lDepth = maxDepth(node.left);
int rDepth = maxDepth(node.right);
/* use the larger one */
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
// Function to return the maximum
// heights among the BSTs
static int maxHeight(int a[], int n)
{
// Create a BST starting from
// the first element
node rootA = null;
rootA = insert(rootA, a[0]);
for (int i = 1; i < n; i++)
rootA = insert(rootA, a[i]);
// Create another BST starting
// from the last element
node rootB = null;
rootB = insert(rootB, a[n - 1]);
for (int i = n - 2; i >= 0; i--)
rootB =insert(rootB, a[i]);
// Find the heights of both the trees
int A = maxDepth(rootA) - 1;
int B = maxDepth(rootB) - 1;
return Math.max(A, B);
}
// Driver code
public static void main(String args[])
{
int a[] = { 2, 1, 3, 4 };
int n = a.length;
System.out.println(maxHeight(a, n));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python implementation of the approach
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
# A utility function to insert
# a new node with given key in BST */
def insert(node: Node, key: int) -> Node:
# If the tree is empty,
# return a new node */
if node is None:
return Node(key)
# Otherwise, recur down the tree
if key < node.key:
node.left = insert(node.left, key)
elif key > node.key:
node.right = insert(node.right, key)
# return the (unchanged) node pointer
return node
# Compute the "maxDepth" of a tree --
# the number of nodes along the longest path
# from the root node down to the farthest leaf node.*/
def maxDepth(node: Node) -> int:
if node is None:
return 0
else:
# compute the depth of each subtree
lDepth = maxDepth(node.left)
rDepth = maxDepth(node.right)
# use the larger one
if lDepth > rDepth:
return lDepth + 1
else:
return rDepth + 1
# Function to return the maximum
# heights among the BSTs
def maxHeight(a: list, n: int) -> int:
# Create a BST starting from
# the first element
rootA = Node(a[0])
for i in range(1, n):
rootA = insert(rootA, a[i])
# Create another BST starting
# from the last element
rootB = Node(a[n - 1])
for i in range(n - 2, -1, -1):
rootB = insert(rootB, a[i])
# Find the heights of both the trees
A = maxDepth(rootA) - 1
B = maxDepth(rootB) - 1
return max(A, B)
# Driver Code
if __name__ == "__main__":
a = [2, 1, 3, 4]
n = len(a)
print(maxHeight(a, n))
# This code is contributed by
# sanjeev2552
C#
// C# implementation of the approach
using System;
class GFG
{
public class node
{
public int key;
public node left, right;
};
// A utility function to create a new BST node
static node newNode(int item)
{
node temp = new node();
temp.key = item;
temp.left = temp.right = null;
return temp;
}
/* A utility function to insert
a new node with given key in BST */
static node insert(node node, int key)
{
/* If the tree is empty,
return a new node */
if (node == null)
return newNode(key);
/* Otherwise, recur down the tree */
if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
/* return the (unchanged) node pointer */
return node;
}
/* Compute the "maxDepth" of a tree --
the number of nodes along the longest path
from the root node down to the farthest leaf node.*/
static int maxDepth(node node)
{
if (node == null)
return 0;
else
{
/* compute the depth of each subtree */
int lDepth = maxDepth(node.left);
int rDepth = maxDepth(node.right);
/* use the larger one */
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
// Function to return the maximum
// heights among the BSTs
static int maxHeight(int []a, int n)
{
// Create a BST starting from
// the first element
node rootA = null;
rootA = insert(rootA, a[0]);
for (int i = 1; i < n; i++)
rootA = insert(rootA, a[i]);
// Create another BST starting
// from the last element
node rootB = null;
rootB = insert(rootB, a[n - 1]);
for (int i = n - 2; i >= 0; i--)
rootB =insert(rootB, a[i]);
// Find the heights of both the trees
int A = maxDepth(rootA) - 1;
int B = maxDepth(rootB) - 1;
return Math.Max(A, B);
}
// Driver code
public static void Main()
{
int []a = { 2, 1, 3, 4 };
int n = a.Length;
Console.WriteLine(maxHeight(a, n));
}
}
// This code is contributed by AnkitRai01
JavaScript
<script>
// Javascript implementation of the approach
class node
{
constructor()
{
this.key = 0;
this.left = null;
this.right = null;
}
};
// A utility function to create a new BST node
function newNode(item)
{
var temp = new node();
temp.key = item;
temp.left = temp.right = null;
return temp;
}
/* A utility function to insert
a new node with given key in BST */
function insert(node, key)
{
/* If the tree is empty,
return a new node */
if (node == null)
return newNode(key);
/* Otherwise, recur down the tree */
if (key < node.key)
node.left = insert(node.left, key);
else if (key > node.key)
node.right = insert(node.right, key);
/* return the (unchanged) node pointer */
return node;
}
/* Compute the "maxDepth" of a tree --
the number of nodes along the longest path
from the root node down to the farthest leaf node.*/
function maxDepth(node)
{
if (node == null)
return 0;
else
{
/* compute the depth of each subtree */
var lDepth = maxDepth(node.left);
var rDepth = maxDepth(node.right);
/* use the larger one */
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
// Function to return the maximum
// heights among the BSTs
function maxHeight(a, n)
{
// Create a BST starting from
// the first element
var rootA = null;
rootA = insert(rootA, a[0]);
for (var i = 1; i < n; i++)
rootA = insert(rootA, a[i]);
// Create another BST starting
// from the last element
var rootB = null;
rootB = insert(rootB, a[n - 1]);
for (var i = n - 2; i >= 0; i--)
rootB =insert(rootB, a[i]);
// Find the heights of both the trees
var A = maxDepth(rootA) - 1;
var B = maxDepth(rootB) - 1;
return Math.max(A, B);
}
// Driver code
var a = [2, 1, 3, 4];
var n = a.length;
document.write(maxHeight(a, n));
// This code is contributed by rrrtnx.
</script>
Similar Reads
Maximum product of any path in given Binary Tree Given a binary tree of N nodes, the task is to find the maximum product of the elements of any path in the binary tree. Note: A path starts from the root and ends at any leaf in the tree. Examples: Input: 4 / \ 2 8 / \ / \2 1 3 4 Output: 128Explanation: Path in the given tree goes like {4, 8, 4} whi
5 min read
Construct a Maximum Binary Tree from two given Binary Trees Given two Binary Trees, the task is to create a Maximum Binary Tree from the two given binary trees and print the Inorder Traversal of that tree. What is the maximum Binary Tree? The maximum binary is constructed in the following manner: In the case of both the Binary Trees having two corresponding
8 min read
Find the maximum GCD of the siblings of a Binary Tree Given a 2d-array arr[][] which represents the nodes of a Binary tree, the task is to find the maximum GCD of the siblings of this tree without actually constructing it. Example:Â Â Input: arr[][] = {{4, 5}, {4, 2}, {2, 3}, {2, 1}, {3, 6}, {3, 12}}Â Output: 6Â Explanation:Â Â For the above tree, the maxi
11 min read
Find the maximum element of every subtree of a Binary Tree Given a Binary Tree, find the maximum element of every subtree of it. Examples :Input : 1 / \ 2 3 / \ / \ 4 5 6 7 Output : [4, 5, 5, 7, 6, 7, 7] Explanation: The maximum element of the subtree rooted at node 4 is 4. The maximum element of the subtree rooted at node 2 is 5. The maximum element of the
15+ min read
Maximum value of Bitwise AND from root to leaf in a Binary tree Given a Binary Tree, the task is to find the maximum value of Bitwise AND from any path from the root node to the leaf node. Examples: Input: Below is the given graph: Output: 7Explanation:path 1: 15->3->5 = (15 & 3 & 5) = 1path 2: 15->3->1 =(15 & 3 & 1) = 1path 3: 15-
7 min read
Height of n-ary tree if parent array is given Given a parent array P, where P[i] indicates the parent of the ith node in the tree(assume parent of root node id indicated with -1). Find the height of the tree. Examples: Input : array[] = [-1 0 1 6 6 0 0 2 7]Output : height = 5Tree formed is: 0 / | \ 5 1 6 / | \ 2 4 3 / 7 / 8 Start at each node a
10 min read