其他分享
首页 > 其他分享> > 270. Closest Binary Search Tree Value

270. Closest Binary Search Tree Value

作者:互联网

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);
    }
}

 

标签:Binary,Search,target,val,preOrder,double,min,Closest,root
来源: https://www.cnblogs.com/feiflytech/p/16142902.html