clueless coding // TODO: be smarter

LeetCode Closest Binary Search Tree Value: Java Solution



TL;DR Code Solution


public int closestValue(TreeNode root, double target) {
    int a = root.val;
    TreeNode kid = target < a ? root.left : root.right;
    if (kid == null) return a;
    int b = closestValue(kid, target);
    return Math.abs(a - target) < Math.abs(b - target) ? a : b;
}