Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7d9dec6

Browse files
committed
closest bst value: done
1 parent 2dd2b81 commit 7d9dec6

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

‎src/main/java/com/leetcode/trees/ClosestBinarySearchTreeValue.java

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,29 @@
1818
public class ClosestBinarySearchTreeValue {
1919

2020
/**
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
2525
* @return
2626
*/
27-
public static TreeNodefindNodeWithClosestValue(TreeNode node, TreeNodeparentNode, intval, intdiff) {
28-
if (node == null) return parentNode;
27+
public static intclosestValue(TreeNode root, doubletarget) {
28+
if (root == null) return -1;
2929

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+
}
3139

3240
if (node.val > val) {
33-
return findNodeWithClosestValue(node.left, node, val, Math.abs(node.val - val));
41+
return closestValue(node.left, closestNode, val);
3442
} else {
35-
return findNodeWithClosestValue(node.right, node, val, Math.abs(node.val - val));
43+
return closestValue(node.right, closestNode, val);
3644
}
3745
}
3846

@@ -54,17 +62,17 @@ public static void main(String[] args) {
5462
root.right = new TreeNode(13);
5563
root.left.left = new TreeNode(5);
5664
root.left.right = new TreeNode(8);
57-
root.left.left.left = new TreeNode(2);
5865
root.left.left.right = new TreeNode(6);
66+
root.left.left.left = new TreeNode(2);
5967
root.right.right = new TreeNode(20);
6068

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
6876

6977
/*
7078
BST looks like:
@@ -83,6 +91,20 @@ public static void main(String[] args) {
8391
root.right.left = new TreeNode(13);
8492
root.right.right = new TreeNode(20);
8593

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /