blob: ab96947ccbbe8db73068a1361895f85dd4c4b2aa (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include "leetcode.h"
class Solution {
public:
int diff = INT_MIN;
int maxAncestorDiff(TreeNode *root) {
if (!root)
return 0;
int mx = INT_MIN, mn = INT_MAX;
getMax(root, mx, mn);
return diff;
}
private:
int getMax(TreeNode *root, int mx, int mn) {
mx = max(mx, root->val);
mn = min(mn, root->val);
diff = max(diff, mx - mn);
getMax(root->left, mx, mn);
getMax(root->right, mx, mn);
}
};
|