原创转载请注明出处:http://agilestyle.iteye.com/blog/2361956
Question:
Find the 'closest' value in a BST with a given value M
Analysis:
1. Traditional Binary Search Tree searching M: O(logN) time to return found M or not
2. "closest" value in BST to M: A particular value in the BST tree so that Math.abs(value-M) should be minimal
Solution:
1. Keep track of the whole searching path to find M and the diff between each value and M
2. Find the value where Math.abs(value-M) is minimal
3. Return min_diff+M as the returned value
package org.fool.java.test; public class FindClosestInBSTTest { public static void main(String[] args) { // 100 // 50 200 // 10 40 150 300 Tree myTree = new Tree(100); myTree.left = new Tree(50); myTree.right = new Tree(200); myTree.left.left = new Tree(10); myTree.left.right = new Tree(40); myTree.right.left = new Tree(150); myTree.right.right = new Tree(300); System.out.println(getCloset(myTree, 120)); System.out.println(getCloset(myTree, 80)); System.out.println(getCloset(myTree, 1000)); System.out.println(getCloset(myTree, 0)); } private static int getCloset(Tree t, int v) { return minDiff(t, v) + v; } private static int minDiff(Tree t, int v) { if (t == null) { return Integer.MAX_VALUE; } if (t.value < v) { return smallDiff(t.value - v, minDiff(t.right, v)); } else { return smallDiff(t.value - v, minDiff(t.left, v)); } } private static int smallDiff(int a, int b) { if (Math.abs(a) > Math.abs(b)) { return b; } return a; } private static class Tree { public int value; public Tree left; public Tree right; public Tree(int value) { this.value = value; this.left = null; this.right = null; } } }
Console Output
Reference
https://www.youtube.com/watch?v=NMdMIrHEA1E&index=37&list=PLlhDxqlV_-vkak9feCSrnjlrnzzzcopSG
相关推荐
java java_leetcode题解之Closest Leaf in a Binary Tree.java
In these systems, packets are transmitted over the same wireless infrastructure used for cellular telephony, with the base station thus being managed by a telecommunications provider. This provides ...
This, combined with the vast amount of dependencies in the kernel and that it is not easy to see all the consequences of a kernel change, demands developers with a relative full understanding of the ...
- FIX: The value of some string flex-properties that began with a parenthese or curly bracket had no apostrophe at the end; that caused an error when reading. (fixed TPropList.SavePropValue for the ...
When teams understand and commit to delivering business value for their customers, when they are free to figure out how to perform tasks, and when they are given the resources they need, they will ...
Matlab有限元网格化源程序-dcircle.m MIT的人写的论文,一个简单的有限元网格化方法 ...• Additional parameters to the functions fd and fh can be given in the last arguments varargin .
When teams understand and commit to delivering business value for their customers, when they are free to figure out how to perform tasks, and when they are given the resources they need, they will ...
这篇文章的标题为《Closest Point Search in Lattices》,即“格点中的最近点搜索”。格点是由一组线性独立的向量构成的,在数学中,特别是在几何、编码理论、密码学等领域有着广泛的应用。文章内容涵盖了对于没有...
Matlab有限元网格化源程序-ddd.m MIT的人写的论文,一个简单的有限元网格化方法 确实可以用,但是我理解...• Additional parameters to the functions fd and fh can be given in the last arguments varargin .
Matlab有限元网格化源程序-huniform.m MIT的人写的论文,一个简单的有限元网格化方法 ...• Additional parameters to the functions fd and fh can be given in the last arguments varargin .
Matlab有限元网格化源程序-distmesh2d.m MIT的人写的论文,一个简单的有限元网格化方法 ...• Additional parameters to the functions fd and fh can be given in the last arguments varargin .
The World Wide Web has been credited with bringing the Internet to the masses. The Internet was previously the stomping ground of academics and a small, elite group of computer professionals, mostly...
NX二次开发UF_EVALSF_find_closest_point 函数介绍,Ufun提供了一系列丰富的 API 函数,可以帮助用户实现自动化、定制化和扩展 NX 软件的功能。无论您是从事机械设计、制造、模具设计、逆向工程、CAE 分析等领域的...
NX二次开发UF_EVALSF_find_closest_point_2 函数介绍,Ufun提供了一系列丰富的 API 函数,可以帮助用户实现自动化、定制化和扩展 NX 软件的功能。无论您是从事机械设计、制造、模具设计、逆向工程、CAE 分析等领域的...
In its basic form, given a new instance, the algorithm finds the \( k \) closest instances (neighbors) in the training set and assigns a label based on the majority vote or a weighted average of the ...
Calculate the distance of a given point P from a triangle TRI. Point P is a row vector of the form 1x3. The triangle is a matrix formed by three rows of points TRI = [P1 P2 P3] each of size 1x3. dist ...
to-fine manner and employs the four neighboring closest data points in the upper layer to make the search region for finding the closest data point corresponding to a model point in the lower layer....
of the path from a given node to the closest goal state. Must be zero if node represents a goal state. -Example: Straight-line distance from current location to the goal location in a road navigation ...