Maximum difference between node and its ancestor | Practice | GeeksforGeeks

Given a Binary Tree, you need to find the maximum value which you can get by subtracting the value of node B from the value of node A, where A and B are two nodes of the binary tree and A is an ancestor of B. 
Example 1:
Input:
    5
 /    \
2      1
Output: 4
Explanation:The maximum difference we can
get is 4, which is bewteen 5 and 1.
Example 2:
Input:
      1
    /    \
   2      3
           \
            7
Output: -1
Explanation:The maximum difference we can
get is -1, which is between 1 and 2.
Your Task:
The task is to complete the function maxDiff() which finds the maximum difference between the node and its ancestor.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(H).
Note: H is the height of the tree.
Constraints:
2 <= Number of edges <= 104
0 <= Data of a node <= 105
Note: The Input/Output format and Examples given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code.
0
abrajput15064 weeks ago
int min3(int a,int b,int c){
    return a<b?(a<c?a:c):(b<c?b:c);
}
int helper(Node* root,int &mx,int mn){
    if(!root)
        return INT_MAX;
    if(!root->left && !root->right)
        return root->data;
        
    int left = helper(root->left,mx,mn);
    int right = helper(root->right,mx,mn);
    mn = min3(root->data,left,right);
    mx = max(root->data - min(left,right),mx);
    return mn;
}
int maxDiff(Node* root)
{
    // Your code here
    int mn = INT_MAX;
    int mx = INT_MIN;
    helper(root,mx,mn);
    return mx;
}
0
ibrahimidn1 month ago
class Tree
{
    int maxSoFar=Integer.MIN_VALUE;
    int maxDiff(Node root)
    {
        if(root==null) return 0;
        findMaxDiff(root.left,root.data);
        findMaxDiff(root.right,root.data);
        return maxSoFar;
    }
    
    void findMaxDiff(Node node,int maxPathValue){
        if(node==null) return;
        int diff=maxPathValue-node.data;
        if(node.data>maxPathValue)
            maxPathValue=node.data;
        maxSoFar=Math.max(maxSoFar,diff);
        findMaxDiff(node.left,maxPathValue);
        findMaxDiff(node.right,maxPathValue);
        
    }
}
+1
ashvinkict201 month ago
int func(Node* root, int &ans){   if(!root)return INT_MAX;   int left = func(root->left,ans);   int right = func(root->right,ans);   ans = max(ans,max(root->data-left,root->data-right));   return min(left,min(root->data,right));}int maxDiff(Node* root){   // Your code here    int ans = INT_MIN;   func(root,ans);   return ans;}
0
nestoffice37311 month ago
4 line java 
int ans;   void solve(Node root,int max){       if(root == null)           return;       solve(root.left,Math.max(max,root.data));       solve(root.right,Math.max(max,root.data));       ans = Math.max(ans,max-root.data);   } 
0
jai20222 months ago
EASY C++ SOLUTION step by stepint diff(Node* root,int &di){   if(root==NULL) return INT_MAX;   if(root->left==NULL && root->right==NULL) return root->data;      int l=diff(root->left,di);   int r=diff(root->right,di);   if(l<r){       di=max(di,root->data-l);   }   else{       di=max(di,root->data-r);   }   return min(min(l,r),root->data); }
int maxDiff(Node* root){   // Your code here    if(root==NULL) return 0;   int di=INT_MIN;   diff(root,di);   return di;}
+2
singhanshul28072 months ago
class Tree
{
   //Function to return the maximum difference between any 
   //node and its ancestor.
   static class Type{
       int max,min,ans;
       public Type(int max,int min,int ans){
           this.max=max;
           this.min=min;
           this.ans=ans;
       }
   }
   public Type solve(Node root){
       if(root==null)return new Type(Integer.MIN_VALUE,Integer.MAX_VALUE,Integer.MIN_VALUE);
       Type left=solve(root.left);
       Type right=solve(root.right);
       
       int child_max=Math.max(left.max,right.max);
       int child_min=Math.min(left.min,right.min);
       int child_ans=Math.max(left.ans,right.ans);
       int curr_max=Math.max(root.data,child_max);
       int curr_min=Math.min(root.data,child_min);
       int ans1=Math.max(root.data-child_max,root.data-child_min);
       int ans=Math.max(ans1,child_ans);
       return new Type(curr_max,curr_min,ans);
   }
   int maxDiff(Node root){
       return solve(root).ans;
   }
}
+1
gurjotsingh21003Premium2 months ago
C++ Solution
int minm(Node* root,int& ans)
{
    if(root==NULL)return INT_MAX;
    
    int lmin=minm(root->left,ans);
    int rmin=minm(root->right,ans);
    
    int val=root->data;
    int temp;
    if(lmin==INT_MAX && rmin==INT_MAX)
    {
        
    }
    else 
    {
        temp=max(val-lmin,val-rmin);
        ans=max(ans,temp);
    }
    return min(val,min(lmin,rmin));
}
int maxDiff(Node* root)
{
    // Your code here 
    if(root==NULL) return 0;
    
    int ans=INT_MIN;
    minm(root,ans);
    return ans;
}
0
hamidnourashraf3 months ago
_MAX = -math.inf
def traverse(root, largest_parent):
   global _MAX
   if root is None:
       return 
   if largest_parent is not None:
       _MAX = max(largest_parent-root.data, _MAX)
   if largest_parent is None:
       largest_parent = root.data
   traverse(root.left, max(largest_parent, root.data))
   traverse(root.right, max(largest_parent, root.data))
def maxDiff(root):
   global _MAX
   _MAX = -math.inf
   traverse(root, None)
   return(_MAX)
+4
17vineet3 months ago
int maxDUtil(Node* root, int& max_diff)
{
    if(!root)
        return INT_MAX ;
    if(!root->left && !root->right)
        return root->data ;
    int left = maxDUtil(root->left,max_diff) ;
    int right = maxDUtil(root->right,max_diff) ;
    max_diff = max(max_diff,root->data-min(left,right)) ;
    return min(min(left,right),root->data) ;
}
int maxDiff(Node* root)
{
    int max_diff = INT_MIN ;
    maxDUtil(root,max_diff) ;
    return max_diff ;
}
0
laxmijha20202020203 months ago
0(1) space , using morris traversal
Node*getrightmostnode(Node*leftnode,Node*curr){   while(leftnode->right!=NULL&&leftnode->right!=curr)   leftnode=leftnode->right;   return leftnode;}int maxDiff(Node* root){  int ans=INT_MIN;  int maximum=root->data;  Node*curr=root;  while(curr!=NULL) {     Node*leftnode=curr->left;     if(leftnode==NULL)     {         if(curr!=root)         {             ans=max(ans,maximum-curr->data);             maximum=max(maximum,curr->data);             curr->data=maximum;         }         curr=curr->right;     }     else{         Node*rightmostnode=getrightmostnode(leftnode,curr);         if(rightmostnode->right==NULL)         {             rightmostnode->right=curr;              if(curr!=root)              {            ans=max(ans,maximum-curr->data);            maximum=max(maximum,curr->data);              }            curr->data=maximum;                        curr=curr->left;         }         else{                          rightmostnode->right=NULL;             maximum=curr->data;             curr=curr->right;         }     } } return ans;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.