
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum element between two nodes of BST in C++
Problem statement
Given an array of N elements and two integers A, B which belongs to the given array. Create a Binary Search Tree by inserting element from arr[0] to arr[n-1]. The task is to find the maximum element in the path from A to B.
Example
If array is {24, 23, 15, 36, 19, 41, 25, 35} the we can form BST as follows −
If we consider A = 19 and B = 41 then maximum element between these two nodes is 41
Algorithm
- Find Lowest Common Ancestor(LCA) of node A and B.
- Find maximum node between LCA and A. Let us call it as max1
- Find maximum node between LCA and B. Let us call it as max2
- Return maximum of max1 and max2
Example
Let us now see an example −
#include <bits/stdc++.h> using namespace std; struct node { int data; struct node* left; struct node* right; }; node *createNode(int x) { node *p = new node(); p -> data = x; p -> left = NULL; 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 getMaximumElement(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[] = {24, 23, 15, 36, 19, 41, 25, 35}; int a = 19, b = 41; 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 << "Maximum element = " << getMaximumElement(root, a, b) << endl; return 0; }
Output
Maximum element = 41
- Related Articles
- C++ program to find the shortest distance between two nodes in BST
- Maximum distance between two occurrences of same element in array in C
- Program to count number of BST with n nodes in Python
- Count BST nodes that lie in a given range in C++
- Find k-th smallest element in BST (Order Statistics in BST) in C++
- Maximum Sum BST in Binary Tree in C++
- Find distance between two nodes of a Binary Tree in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Kth Smallest Element in a BST in Python
- Find distance between two nodes of a Binary Tree in C++ Program
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Program to remove all nodes from BST which are not in range in Python
- Program to find longest path between two nodes of a tree in Python
- Maximum difference between two elements such that larger element appears after the smaller number in C
- XOR of the path between any two nodes in a Binary Tree in C++

Advertisements