@phantom How to approach this question?
https://practice.geeksforgeeks.org/problems/maximum-difference-between-node-and-its-ancestor/1
@phantom How to approach this question?
https://practice.geeksforgeeks.org/problems/maximum-difference-between-node-and-its-ancestor/1
just take a node it act as ancestor to all below nodes then find the diff. and do it for all node
it a o(n^2) approach
int find_dist(Node* root,Node* root2){
if(root==NULL){
return 0;
}
return max(find_dist(root->left,root),max(find_dist(root->right,root),root2->data-root->data));
}
int maxDiff(Node* root)
{
int fans=INT_MIN;
if(root==NULL){
return 0;
}
int ans=find_dist(root,root);
int op1=maxDiff(root->left);
int op2=maxDiff(root->right);
return max(op1,max(op2,ans));
//cout<<op1<<" "<<op2<<endl;
// Your code here
}