Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode() : val(0), left(nullptr), right(nullptr) {}
- * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
- * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
- * };
- */
- class Solution {
- public:
- int maxAncestorDiff(TreeNode* root) {
- return dfs(root, root->val, root->val);
- }
- int dfs(TreeNode* node, int curMax, int curMin){
- if(!node) return curMax-curMin;
- curMax = max(curMax, node->val);
- curMin = min(curMin, node->val);
- return max(dfs(node->left, curMax, curMin), dfs(node->right, curMax, curMin));
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement