Maximum absolute difference between the sibling nodes of given BST
Last Updated : 23 Jul, 2025
Given a BST (Binary Search Tree) with N Nodes, the task is to find the maximum absolute difference between the sibling nodes.
Two nodes are said to be siblings if they are present at the same level, and their parents are the same.]
Examples:
Input:
Diagram 1 for Ex-1
Output: 70 Explanation: 105 - 50 = 55 (As 105 and 50 nodes are siblings) 75 - 5 = 70 (As 75 and 5 nodes are siblings) 106 - 101 = 5 (As 106 and 5 nodes are siblings) Other than these, there is no other pair of nodes that are siblings. So among them the max difference is 70. (75 - 5 = 70)
Input:
Diagram 2 for Ex-2
Output: 60 Explanation: 120 - 60 = 60 90 - 45 = 45 160 - 110 = 50 Other than these, there is no other pair of nodes that are siblings. So among them the max difference is 60. (120 - 60 = 60)
Approach:
The basic Idea is to check whether the nodes are siblings or not if sibling then find out the maximum difference.
Follow the steps mentioned below to implement the idea:
Iterate the BST in the preorder fashion. Root Left Right.
At every point,
Check if the left and right children are available.
If both are available, then calculate the difference between the right and left values. (By default BST right val > left val)
Maximize the absolute maximum difference.
Else just continue the traversal.
After the traversal ends, return the maximum difference between the sibling nodes.
Below is the Implementation of the above approach:
C++
// C++ code to implement the approach#include<bits/stdc++.h>usingnamespacestd;// Structure of a BST nodestructTreeNode{intval;structTreeNode*left,*right;};// Function to form a new nodeTreeNode*newNode(intdata){TreeNode*temp=newTreeNode;temp->val=data;temp->left=NULL;temp->right=NULL;returntemp;}// Function to insert a new node to the BSTTreeNode*insert(TreeNode*root,intval){TreeNode*newnode=newNode(val);TreeNode*x=root;TreeNode*y=NULL;while(x!=NULL){y=x;if(val<x->val)x=x->left;elsex=x->right;}if(y==NULL)y=newnode;elseif(val<y->val)y->left=newnode;elsey->right=newnode;returny;}// Function to find the maximum absolute differencevoidfindMaxDiff(TreeNode*root,int&max_diff){if(root==NULL){return;}intlval,rval;// Only when both siblings are thereif(root->left!=NULL&&root->right!=NULL){lval=root->left->val;rval=root->right->val;max_diff=max(max_diff,rval-lval);}findMaxDiff(root->left,max_diff);findMaxDiff(root->right,max_diff);}// Driver codeintmain(){TreeNode*root=NULL;TreeNode*root2=NULL;// Structure of first treeroot=insert(root,100);insert(root,50);insert(root,105);insert(root,75);insert(root,5);insert(root,101);insert(root,106);// Structure of second treeroot2=insert(root2,100);insert(root2,60);insert(root2,120);insert(root2,45);insert(root2,90);insert(root2,110);insert(root2,160);intmax_diff=INT_MIN;intmax_diff2=INT_MIN;findMaxDiff(root,max_diff);cout<<max_diff<<"\n";findMaxDiff(root2,max_diff2);cout<<max_diff2<<"\n";return0;}
Java
// java code implementationimportjava.util.Scanner;classTreeNode{intval;TreeNodeleft,right;TreeNode(intval){this.val=val;this.left=null;this.right=null;}}classGFG{// Function to form a new nodepublicstaticTreeNodenewNode(intdata){TreeNodetemp=newTreeNode(data);returntemp;}// Function to insert a new node to the BSTpublicstaticTreeNodeinsert(TreeNoderoot,intval){TreeNodenewnode=newNode(val);TreeNodex=root;TreeNodey=null;while(x!=null){y=x;if(val<x.val)x=x.left;elsex=x.right;}if(y==null)y=newnode;elseif(val<y.val)y.left=newnode;elsey.right=newnode;returny;}// Function to find the maximum absolute differencepublicstaticvoidfindMaxDiff(TreeNoderoot,int[]max_diff){if(root==null){return;}intlval,rval;// Only when both siblings are thereif(root.left!=null&&root.right!=null){lval=root.left.val;rval=root.right.val;max_diff[0]=Math.max(max_diff[0],rval-lval);}findMaxDiff(root.left,max_diff);findMaxDiff(root.right,max_diff);}// Driver codepublicstaticvoidmain(String[]args){TreeNoderoot=null;TreeNoderoot2=null;// Structure of first treeroot=insert(root,100);insert(root,50);insert(root,105);insert(root,75);insert(root,5);insert(root,101);insert(root,106);// Structure of second treeroot2=insert(root2,100);insert(root2,60);insert(root2,120);insert(root2,45);insert(root2,90);insert(root2,110);insert(root2,160);int[]max_diff={Integer.MIN_VALUE};int[]max_diff2={Integer.MIN_VALUE};findMaxDiff(root,max_diff);System.out.println(max_diff[0]);findMaxDiff(root2,max_diff2);System.out.println(max_diff2[0]);}}// This code is contributed by ksam24000
Python3
# Python code implementationclassTreeNode:def__init__(self,val):self.val=valself.left=Noneself.right=None# Function to form a new nodedefnewNode(data):temp=TreeNode(data)returntemp# Function to insert a new node to the BSTdefinsert(root,val):newnode=newNode(val)x=rooty=NonewhilexisnotNone:y=xifval<x.val:x=x.leftelse:x=x.rightifyisNone:y=newnodeelifval<y.val:y.left=newnodeelse:y.right=newnodereturny# Function to find the maximum absolute differencedeffindMaxDiff(root,max_diff):ifrootisNone:returnlval,rval=None,None# Only when both siblings are thereifroot.leftisnotNoneandroot.rightisnotNone:lval=root.left.valrval=root.right.valmax_diff[0]=max(max_diff[0],rval-lval)findMaxDiff(root.left,max_diff)findMaxDiff(root.right,max_diff)root=Noneroot2=None# Structure of first treeroot=insert(root,100)insert(root,50)insert(root,105)insert(root,75)insert(root,5)insert(root,101)insert(root,106)# Structure of second treeroot2=insert(root2,100)insert(root2,60)insert(root2,120)insert(root2,45)insert(root2,90)insert(root2,110)insert(root2,160)max_diff=[float("-inf")]max_diff2=[float("-inf")]findMaxDiff(root,max_diff)print(max_diff[0])findMaxDiff(root2,max_diff2)print(max_diff2[0])# This code is contributed by lokesh.
C#
// C# code implementationusingSystem;classTreeNode{publicintval;publicTreeNodeleft,right;publicTreeNode(intval){this.val=val;this.left=null;this.right=null;}}publicclassGFG{// Function to form a new nodestaticTreeNodenewNode(intdata){TreeNodetemp=newTreeNode(data);returntemp;}// Function to insert a new node to the BSTstaticTreeNodeinsert(TreeNoderoot,intval){TreeNodenewnode=newNode(val);TreeNodex=root;TreeNodey=null;while(x!=null){y=x;if(val<x.val)x=x.left;elsex=x.right;}if(y==null)y=newnode;elseif(val<y.val)y.left=newnode;elsey.right=newnode;returny;}// Function to find the maximum absolute differencestaticvoidfindMaxDiff(TreeNoderoot,int[]max_diff){if(root==null){return;}intlval,rval;// Only when both siblings are thereif(root.left!=null&&root.right!=null){lval=root.left.val;rval=root.right.val;max_diff[0]=Math.Max(max_diff[0],rval-lval);}findMaxDiff(root.left,max_diff);findMaxDiff(root.right,max_diff);}staticpublicvoidMain(){// CodeTreeNoderoot=null;TreeNoderoot2=null;// Structure of first treeroot=insert(root,100);insert(root,50);insert(root,105);insert(root,75);insert(root,5);insert(root,101);insert(root,106);// Structure of second treeroot2=insert(root2,100);insert(root2,60);insert(root2,120);insert(root2,45);insert(root2,90);insert(root2,110);insert(root2,160);int[]max_diff={Int32.MinValue};int[]max_diff2={Int32.MinValue};findMaxDiff(root,max_diff);Console.WriteLine(max_diff[0]);findMaxDiff(root2,max_diff2);Console.WriteLine(max_diff2[0]);}}// This code is contributed by lokeshmvs21.
JavaScript
// JavaScript Code to implement the approach// Structure of a BST nodeclassTreeNode{constructor(data){this.val=data;this.left=null;this.right=null;}}// Function to insert a new node to the BSTfunctioninsert(root,val){letnewnode=newTreeNode(val);letx=root;lety=null;while(x!=null){y=x;if(val<x.val)x=x.left;elsex=x.right;}if(y==null){y=newnode;}elseif(val<y.val){y.left=newnode;}else{y.right=newnode;}returny;}// Function to find the maximum absolute differencefunctionfindMaxDiff(root,max_diff){if(root==null)return;letlval,rval;// Only when both siblings are thereif(root.left!=null&&root.right!=null){lval=root.left.val;rval=root.right.val;max_diff[0]=Math.max(max_diff[0],rval-lval);}findMaxDiff(root.left,max_diff);findMaxDiff(root.right,max_diff);}// Driver Codeletroot=null;letroot2=null;// structure of first treeroot=insert(root,100);insert(root,50);insert(root,105);insert(root,75);insert(root,5);insert(root,101);insert(root,106);// Structure of second treeroot2=insert(root2,100);insert(root2,60);insert(root2,120);insert(root2,45);insert(root2,90);insert(root2,110);insert(root2,160);letmax_diff=[Number.MIN_VALUE];letmax_diff2=[Number.MIN_VALUE];findMaxDiff(root,max_diff);document.write(max_diff[0]);document.write("<br>");findMaxDiff(root2,max_diff2);document.write(max_diff2[0]);// This code is contributed by Yash Agarwal