-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaximum-difference-between-node-and-ancestor.cpp
More file actions
39 lines (38 loc) · 1.11 KB
/
maximum-difference-between-node-and-ancestor.cpp
File metadata and controls
39 lines (38 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int res=0;
int maxAncestorDiff(TreeNode* root) {
if(!root) return 0;
dfs(root);
return res;
}
pair<int,int> dfs(TreeNode* root){
if(!root) return make_pair<int,int>(INT_MAX,INT_MIN);
if(!root->left && !root->right) return make_pair(root->val,root->val);
auto l=dfs(root->left);
auto r=dfs(root->right);
int v=root->val;
if(l.first!=INT_MAX) res=max(res,abs(v-l.first));
if(l.second!=INT_MIN) res=max(res,abs(v-l.second));
if(r.first!=INT_MAX) res=max(res,abs(v-r.first));
if(r.second!=INT_MIN) res=max(res,abs(v-r.second));
int minv=INT_MAX;
int maxv=INT_MIN;
minv=min(minv,l.first);
minv=min(minv,r.first);
minv=min(minv,v);
maxv=max(maxv,l.second);
maxv=max(maxv,r.second);
maxv=max(maxv,v);
return make_pair(minv,maxv);
}
};