Given an array of N elements and two integers A, B which belong to the given array. Create a Binary Search Tree by inserting elements from arr[0] to arr[n-1]. The task is to find the maximum element in the path from A to B.
Examples :
Input : arr[] = { 18, 36, 9, 6, 12, 10, 1, 8 },
a = 1,
b = 10.
Output : 12

Path from 1 to 10 contains { 1, 6, 9, 12, 10 }. The maximum element is 12.
The idea is to find Lowest Common Ancestor of node ‘a’ and node ‘b’. Then search maximum node between LCA and ‘a’, and also find the maximum node between LCA and ‘b’. The answer will be maximum node of two.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
struct Node *left, *right;
int data;
};
Node *createNode( int x)
{
Node *p = new Node;
p -> data = x;
p -> left = p -> right = NULL;
return p;
}
void insertNode( struct Node *root, int x)
{
Node *p = root, *q = NULL;
while (p != NULL)
{
q = p;
if (p -> data < x)
p = p -> right;
else
p = p -> left;
}
if (q == NULL)
p = createNode(x);
else
{
if (q -> data < x)
q -> right = createNode(x);
else
q -> left = createNode(x);
}
}
int maxelpath(Node *q, int x)
{
Node *p = q;
int mx = INT_MIN;
while (p -> data != x)
{
if (p -> data > x)
{
mx = max(mx, p -> data);
p = p -> left;
}
else
{
mx = max(mx, p -> data);
p = p -> right;
}
}
return max(mx, x);
}
int maximumElement( struct Node *root, int x, int y)
{
Node *p = root;
while ((x < p -> data && y < p -> data) ||
(x > p -> data && y > p -> data))
{
if (x < p -> data && y < p -> data)
p = p -> left;
else if (x > p -> data && y > p -> data)
p = p -> right;
}
return max(maxelpath(p, x), maxelpath(p, y));
}
int main()
{
int arr[] = { 18, 36, 9, 6, 12, 10, 1, 8 };
int a = 1, b = 10;
int n = sizeof (arr) / sizeof (arr[0]);
struct Node *root = createNode(arr[0]);
for ( int i = 1; i < n; i++)
insertNode(root, arr[i]);
cout << maximumElement(root, a, b) << endl;
return 0;
}
|
Java
class Solution
{
static class Node
{
Node left, right;
int data;
}
static Node createNode( int x)
{
Node p = new Node();
p . data = x;
p . left = p . right = null ;
return p;
}
static void insertNode( Node root, int x)
{
Node p = root, q = null ;
while (p != null )
{
q = p;
if (p . data < x)
p = p . right;
else
p = p . left;
}
if (q == null )
p = createNode(x);
else
{
if (q . data < x)
q . right = createNode(x);
else
q . left = createNode(x);
}
}
static int maxelpath(Node q, int x)
{
Node p = q;
int mx = - 1 ;
while (p . data != x)
{
if (p . data > x)
{
mx = Math.max(mx, p . data);
p = p . left;
}
else
{
mx = Math.max(mx, p . data);
p = p . right;
}
}
return Math.max(mx, x);
}
static int maximumElement( Node root, int x, int y)
{
Node p = root;
while ((x < p . data && y < p . data) ||
(x > p . data && y > p . data))
{
if (x < p . data && y < p . data)
p = p . left;
else if (x > p . data && y > p . data)
p = p . right;
}
return Math.max(maxelpath(p, x), maxelpath(p, y));
}
public static void main(String args[])
{
int arr[] = { 18 , 36 , 9 , 6 , 12 , 10 , 1 , 8 };
int a = 1 , b = 10 ;
int n =arr.length;
Node root = createNode(arr[ 0 ]);
for ( int i = 1 ; i < n; i++)
insertNode(root, arr[i]);
System.out.println( maximumElement(root, a, b) );
}
}
|
Python3
class createNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def insertNode(root, x):
p, q = root, None
while p ! = None :
q = p
if p.data < x:
p = p.right
else :
p = p.left
if q = = None :
p = createNode(x)
else :
if q.data < x:
q.right = createNode(x)
else :
q.left = createNode(x)
def maxelpath(q, x):
p = q
mx = - 999999999999
while p.data ! = x:
if p.data > x:
mx = max (mx, p.data)
p = p.left
else :
mx = max (mx, p.data)
p = p.right
return max (mx, x)
def maximumElement(root, x, y):
p = root
while ((x < p.data and y < p.data) or
(x > p.data and y > p.data)):
if x < p.data and y < p.data:
p = p.left
elif x > p.data and y > p.data:
p = p.right
return max (maxelpath(p, x), maxelpath(p, y))
if __name__ = = '__main__' :
arr = [ 18 , 36 , 9 , 6 , 12 , 10 , 1 , 8 ]
a, b = 1 , 10
n = len (arr)
root = createNode(arr[ 0 ])
for i in range ( 1 ,n):
insertNode(root, arr[i])
print (maximumElement(root, a, b))
|
C#
using System;
public class Solution
{
public class Node
{
public Node left, right;
public int data;
}
public static Node createNode( int x)
{
Node p = new Node();
p.data = x;
p.left = p.right = null ;
return p;
}
public static void insertNode(Node root, int x)
{
Node p = root, q = null ;
while (p != null )
{
q = p;
if (p.data < x)
{
p = p.right;
}
else
{
p = p.left;
}
}
if (q == null )
{
p = createNode(x);
}
else
{
if (q.data < x)
{
q.right = createNode(x);
}
else
{
q.left = createNode(x);
}
}
}
public static int maxelpath(Node q, int x)
{
Node p = q;
int mx = -1;
while (p.data != x)
{
if (p.data > x)
{
mx = Math.Max(mx, p.data);
p = p.left;
}
else
{
mx = Math.Max(mx, p.data);
p = p.right;
}
}
return Math.Max(mx, x);
}
public static int maximumElement(Node root, int x, int y)
{
Node p = root;
while ((x < p.data && y < p.data) || (x > p.data && y > p.data))
{
if (x < p.data && y < p.data)
{
p = p.left;
}
else if (x > p.data && y > p.data)
{
p = p.right;
}
}
return Math.Max(maxelpath(p, x), maxelpath(p, y));
}
public static void Main( string [] args)
{
int [] arr = new int [] {18, 36, 9, 6, 12, 10, 1, 8};
int a = 1, b = 10;
int n = arr.Length;
Node root = createNode(arr[0]);
for ( int i = 1; i < n; i++)
{
insertNode(root, arr[i]);
}
Console.WriteLine(maximumElement(root, a, b));
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
function createNode(x) {
var p = new Node();
p.data = x;
p.left = p.right = null ;
return p;
}
function insertNode(root , x) {
var p = root, q = null ;
while (p != null ) {
q = p;
if (p.data < x)
p = p.right;
else
p = p.left;
}
if (q == null )
p = createNode(x);
else {
if (q.data < x)
q.right = createNode(x);
else
q.left = createNode(x);
}
}
function maxelpath(q , x) {
var p = q;
var mx = -1;
while (p.data != x) {
if (p.data > x) {
mx = Math.max(mx, p.data);
p = p.left;
} else {
mx = Math.max(mx, p.data);
p = p.right;
}
}
return Math.max(mx, x);
}
function maximumElement(root , x , y) {
var p = root;
while ((x < p.data && y < p.data) ||
(x > p.data && y > p.data)) {
if (x < p.data && y < p.data)
p = p.left;
else if (x > p.data && y > p.data)
p = p.right;
}
return Math.max(maxelpath(p, x), maxelpath(p, y));
}
var arr = [ 18, 36, 9, 6, 12, 10, 1, 8 ];
var a = 1, b = 10;
var n = arr.length;
var root = createNode(arr[0]);
for (i = 1; i < n; i++)
insertNode(root, arr[i]);
document.write(maximumElement(root, a, b));
</script>
|
Time complexity: O(h), where h is the height of BST
Auxiliary Space: O(1)
Approach: Use a hash table to store the parent node of each node in the binary search tree. We can start from both the given nodes and traverse up the tree, storing the nodes encountered in a set. Once we reach the root or a common ancestor of the two nodes, we can traverse down the tree from each node and find the maximum element encountered in the set of nodes.
Algorithm steps for the above approach:
- Create an empty hash table to store the parent node of each node in the binary search tree.
- Perform a depth-first search (DFS) traversal of the binary search tree and populate the hash table with the parent node of each node.
- Initialize two pointers, say p1 and p2, to the given nodes.
- Initialize two empty sets, say s1 and s2, to store the nodes encountered while traversing up the tree from p1 and p2, respectively.
- While p1 and p2 are not equal, do the following:
a. If p1 is not null, add it to set s1 and update p1 to its parent node using the hash table.
b. If p2 is not null, add it to set s2 and update p2 to its parent node using the hash table. - Find the intersection set of s1 and s2, i.e., the set of nodes that are common to both s1 and s2.
- Initialize a variable, say max_element, to the minimum integer value.
- For each node in the intersection set, traverse down the tree from the node to the given nodes and find the maximum element encountered. Update max_element if a greater element is encountered.
- Return max_element.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
struct Node *left, *right;
int data;
};
Node *createNode( int x)
{
Node *p = new Node;
p -> data = x;
p -> left = p -> right = NULL;
return p;
}
void insertNode( struct Node *root, int x)
{
Node *p = root, *q = NULL;
while (p != NULL)
{
q = p;
if (p -> data < x)
p = p -> right;
else
p = p -> left;
}
if (q == NULL)
p = createNode(x);
else
{
if (q -> data < x)
q -> right = createNode(x);
else
q -> left = createNode(x);
}
}
int maxelpath(Node *q, int x)
{
Node *p = q;
int mx = INT_MIN;
while (p -> data != x)
{
if (p -> data > x)
{
mx = max(mx, p -> data);
p = p -> left;
}
else
{
mx = max(mx, p -> data);
p = p -> right;
}
}
return max(mx, x);
}
int maximumElement( struct Node *root, int x, int y)
{
Node *p = root;
while ((x < p -> data && y < p -> data) ||
(x > p -> data && y > p -> data))
{
if (x < p -> data && y < p -> data)
p = p -> left;
else if (x > p -> data && y > p -> data)
p = p -> right;
}
return max(maxelpath(p, x), maxelpath(p, y));
}
int main()
{
int arr[] = { 18, 36, 9, 6, 12, 10, 1, 8 };
int a = 1, b = 10;
int n = sizeof (arr) / sizeof (arr[0]);
struct Node *root = createNode(arr[0]);
for ( int i = 1; i < n; i++)
insertNode(root, arr[i]);
cout << maximumElement(root, a, b) << endl;
return 0;
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def insertNode(root, x):
if root is None :
return Node(x)
if x < root.data:
root.left = insertNode(root.left, x)
elif x > root.data:
root.right = insertNode(root.right, x)
return root
def maxelpath(q, x):
p = q
mx = float ( '-inf' )
while p.data ! = x:
if p.data > x:
mx = max (mx, p.data)
p = p.left
else :
mx = max (mx, p.data)
p = p.right
return max (mx, x)
def maximumElement(root, x, y):
p = root
while (x < p.data and y < p.data) or (x > p.data and y > p.data):
if x < p.data and y < p.data:
p = p.left
elif x > p.data and y > p.data:
p = p.right
return max (maxelpath(p, x), maxelpath(p, y))
arr = [ 18 , 36 , 9 , 6 , 12 , 10 , 1 , 8 ]
a, b = 1 , 10
n = len (arr)
root = None
for i in range (n):
root = insertNode(root, arr[i])
print (maximumElement(root, a, b))
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
function insertNode(root, x) {
if (root === null ) {
return new Node(x);
}
if (x < root.data) {
root.left = insertNode(root.left, x);
} else if (x > root.data) {
root.right = insertNode(root.right, x);
}
return root;
}
function maxelpath(q, x) {
let p = q;
let mx = Number.MIN_SAFE_INTEGER;
while (p.data !== x) {
if (p.data > x) {
mx = Math.max(mx, p.data);
p = p.left;
} else {
mx = Math.max(mx, p.data);
p = p.right;
}
}
return Math.max(mx, x);
}
function maximumElement(root, x, y) {
let p = root;
while ((x < p.data && y < p.data) || (x > p.data && y > p.data)) {
if (x < p.data && y < p.data) {
p = p.left;
}
else if (x > p.data && y > p.data) {
p = p.right;
}
}
return Math.max(maxelpath(p, x), maxelpath(p, y));
}
const arr = [18, 36, 9, 6, 12, 10, 1, 8];
const a = 1, b = 10;
const n = arr.length;
let root = null ;
for (let i = 0; i < n; i++) {
root = insertNode(root, arr[i]);
}
console.log(maximumElement(root, a, b));
|
Output:
12
Time complexity: O(n), where n is the number of nodes in the binary search tree.
Auxiliary Space: O(n)
This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.