18
18
public class ClosestBinarySearchTreeValue {
19
19
20
20
/**
21
- * @param node
22
- * @param parentNode
23
- * @param val
24
- * @param diff
21
+ * Runtime: <a href="https://leetcode.com/submissions/detail/248841443/">0 ms</a>.
22
+ *
23
+ * @param root
24
+ * @param target
25
25
* @return
26
26
*/
27
- public static TreeNode findNodeWithClosestValue (TreeNode node , TreeNode parentNode , int val , int diff ) {
28
- if (node == null ) return parentNode ;
27
+ public static int closestValue (TreeNode root , double target ) {
28
+ if (root == null ) return - 1 ;
29
29
30
- if (Math .abs (node .val - val ) > diff ) return parentNode ;
30
+ return closestValue (root , root , target );
31
+ }
32
+
33
+ private static int closestValue (TreeNode node , TreeNode closestNode , double val ) {
34
+ if (node == null ) return closestNode .val ;
35
+
36
+ if (Math .abs (node .val - val ) < Math .abs (closestNode .val - val )) {
37
+ closestNode = node ;
38
+ }
31
39
32
40
if (node .val > val ) {
33
- return findNodeWithClosestValue (node .left , node , val , Math . abs ( node . val - val ) );
41
+ return closestValue (node .left , closestNode , val );
34
42
} else {
35
- return findNodeWithClosestValue (node .right , node , val , Math . abs ( node . val - val ) );
43
+ return closestValue (node .right , closestNode , val );
36
44
}
37
45
}
38
46
@@ -54,17 +62,17 @@ public static void main(String[] args) {
54
62
root .right = new TreeNode (13 );
55
63
root .left .left = new TreeNode (5 );
56
64
root .left .right = new TreeNode (8 );
57
- root .left .left .left = new TreeNode (2 );
58
65
root .left .left .right = new TreeNode (6 );
66
+ root .left .left .left = new TreeNode (2 );
59
67
root .right .right = new TreeNode (20 );
60
68
61
- assertEquals (13 , findNodeWithClosestValue (root , root , 15 , Integer . MAX_VALUE ). val );
62
- assertEquals (13 , findNodeWithClosestValue (root , root , 13 , Integer . MAX_VALUE ). val );
63
- assertEquals (9 , findNodeWithClosestValue (root , root , 9 , Integer . MAX_VALUE ). val );
64
- assertEquals (2 , findNodeWithClosestValue (root , root , 2 , Integer . MAX_VALUE ). val );
65
- assertEquals (2 , findNodeWithClosestValue (root , root , 1 , Integer . MAX_VALUE ). val );
66
- assertEquals (6 , findNodeWithClosestValue (root , root , 6 , Integer . MAX_VALUE ). val );
67
- assertEquals (13 , findNodeWithClosestValue (root , root , 11 , Integer . MAX_VALUE ). val ); // tie b/w 9 and 13
69
+ assertEquals (13 , closestValue (root , 15 ) );
70
+ assertEquals (13 , closestValue (root , 13 ) );
71
+ assertEquals (9 , closestValue (root , 9 ) );
72
+ assertEquals (2 , closestValue (root , 2 ) );
73
+ assertEquals (2 , closestValue (root , 1 ) );
74
+ assertEquals (6 , closestValue (root , 6 ) );
75
+ assertEquals (13 , closestValue (root , 11 ) ); // tie b/w 9 and 13
68
76
69
77
/*
70
78
BST looks like:
@@ -83,6 +91,20 @@ public static void main(String[] args) {
83
91
root .right .left = new TreeNode (13 );
84
92
root .right .right = new TreeNode (20 );
85
93
86
- assertEquals (13 , findNodeWithClosestValue (root , root , 15 , Integer .MAX_VALUE ).val );
94
+ assertEquals (13 , closestValue (root , 15 ));
95
+
96
+ /*
97
+ BST looks like:
98
+
99
+ 1500000000
100
+ /
101
+ /
102
+ /
103
+ 1400000000
104
+ */
105
+ root = new TreeNode (1500000000 );
106
+ root .left = new TreeNode (1400000000 );
107
+
108
+ assertEquals (1400000000 , closestValue (root , -1500000000.0 ));
87
109
}
88
110
}
0 commit comments