class Solution {
double min;
int result;
public int closestValue(TreeNode root, double target) {
min = Math.abs(root.val-target);
inorder(root,target);
return result;
}
public void inorder(TreeNode root,double target) {//需要传入target值
if (root == null) {
return;
}
inorder(root.left,target);
if (Math.abs(target-root.val) <= min) {
min=Math.abs(target-root.val);//min需要赋值,勿忘
result = root.val;
}
inorder(root.right,target);
}
}