Given the root of a binary tree, find the maximum value v for which there exist different nodes a and b where v = |a.val - b.val| and a is an ancestor of b.

A node a is an ancestor of b if either: any child of a is equal to b&nbsp;or any child of a is an ancestor of b.

&nbsp;
Example 1:

Input: root = [8,3,10,1,6,null,14,null,null,4,7,13]
Output: 7
Explanation: We have various ancestor-node differences, some of which are given below :
|8 - 3| = 5
|3 - 7| = 4
|8 - 1| = 7
|10 - 13| = 3
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.

Example 2:

Input: root = [1,null,2,null,0,3]
Output: 3


&nbsp;
Constraints:


	The number of nodes in the tree is in the range [2, 5000].
	0 &lt;= Node.val &lt;= 105


class Solution:
    def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:
        self.max_diff = float('-inf')
        
        def dfs(node,prev_min,prev_max):
            if not node:
                return
            dfs(node.left,min(prev_min,node.val),max(prev_max,node.val))
            dfs(node.right,min(prev_min,node.val),max(prev_max,node.val))
            self.max_diff = max(abs(node.val-prev_min),abs(node.val-prev_max),self.max_diff)
        dfs(root,root.val,root.val)
        return self.max_diff
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    public int maxAncestorDiff(TreeNode root) {

        if (root == null) return 0;

        return find(root, Integer.MAX_VALUE, Integer.MIN_VALUE);
    }

    public int find(TreeNode root, int min, int max) {
        if (root == null) return Math.abs(max-min);

        min = Math.min(min, root.val);
        max = Math.max(max, root.val);

        return Math.max(find(root.left, min, max), find(root.right, min, max));
    }

}
class Solution {
private:
    int maxDiff;
    pair <int, int> helper(TreeNode* root) {
        if (root == NULL) return {INT_MAX, INT_MIN};
        pair <int, int> L = helper(root -> left), R = helper(root -> right);
        pair <int, int> minMax = {min(L.first, R.first), max(L.second, R.second)};
        if (minMax.first != INT_MAX) maxDiff = max(maxDiff, max(abs(root -> val - minMax.first), abs(root -> val - minMax.second)));
        return {min(root -> val, minMax.first), max(root -> val, minMax.second)};
    }
public:
    int maxAncestorDiff(TreeNode* root) {
        maxDiff = INT_MIN;
        helper(root);
        return maxDiff;
    }
};
var maxAncestorDiff = function(root) {
    let ans = 0;
    const traverse = (r = root, mx = root.val, mn = root.val) => {
        if(!r) return;
        ans = Math.max(ans, Math.abs(mx - r.val), Math.abs(mn - r.val));
        mx = Math.max(mx, r.val);
        mn = Math.min(mn, r.val);
        traverse(r.left, mx, mn);
        traverse(r.right, mx, mn);
    }
    traverse();
    return ans;
};
Maximum Difference Between Node and Ancestor