0ms Java Solution


  • 0
    W
    public class Solution {
        public int cur = -1;
        public void trav(TreeNode n, double t) {
            if (n==null) return;
            if (Math.abs( n.val - t) < Math.abs( cur - t) ) cur = n.val;
            if (n.left != null && t<n.val) trav(n.left,t);
            if (n.right!=null && t>n.val) trav(n.right,t);
        }
        public int closestValue(TreeNode root, double target) {
            cur = root.val;
            trav(root, target);
            return cur;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.