PreOrder:
class Solution { double min = Integer.MAX_VALUE; int val; public int closestValue(TreeNode root, double target) { preOrder(root, target); return val; } private void preOrder(TreeNode root, double target){ if(root==null) return; if(Math.abs(root.val-target)< min){ min = Math.abs(root.val-target); val = root.val; } preOrder(root.left, target); preOrder(root.right, target); } }