C/C++教程

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

 

这篇关于270. Closest Binary Search Tree Value的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!