每日一题1026. 节点与其祖先之间的最大差值
题目链接:https://leetcode.cn/problems/maximum-difference-between-node-and-ancestor/description/
思路
今天的每日一题较简单
class Solution {
public int maxAncestorDiff(TreeNode root) {
return back(root,root.val,root.val);
}
private int back(TreeNode node,int max,int min){
if (node==null){
return 0;
}
int num = Math.max(Math.abs(max- node.val),Math.abs(min- node.val));
max = Math.max(max, node.val);
min = Math.min(min, node.val);
int l_num = back(node.left,max,min);
int r_num = back(node.right,max,min);
return Math.max(num,Math.max(l_num,r_num));
}
}