forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClosestBinarySearchTreeValueII.java
76 lines (66 loc) · 2.33 KB
/
ClosestBinarySearchTreeValueII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package com.leetcode.trees;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Level: Hard
* Problem Link: https://leetcode.com/problems/closest-binary-search-tree-value-ii/
* Problem Description:
*
*
* @author rampatra
* @since 2019-07-31
*/
public class ClosestBinarySearchTreeValueII {
/**
* @param node
* @param parentNode
* @param val
* @param diff
* @return
*/
public static TreeNode findNodeWithClosestValue(TreeNode node, TreeNode parentNode, int val, int diff) {
return null;
}
public static void main(String[] args) {
/*
BST looks like:
9
/ \
7 13
/ \ \
5 8 20
/ \
2 6
*/
TreeNode root = new TreeNode(9);
root.left = new TreeNode(7);
root.right = new TreeNode(13);
root.left.left = new TreeNode(5);
root.left.right = new TreeNode(8);
root.left.left.left = new TreeNode(2);
root.left.left.right = new TreeNode(6);
root.right.right = new TreeNode(20);
assertEquals(13, findNodeWithClosestValue(root, root, 15, Integer.MAX_VALUE).val);
assertEquals(13, findNodeWithClosestValue(root, root, 13, Integer.MAX_VALUE).val);
assertEquals(9, findNodeWithClosestValue(root, root, 9, Integer.MAX_VALUE).val);
assertEquals(2, findNodeWithClosestValue(root, root, 2, Integer.MAX_VALUE).val);
assertEquals(2, findNodeWithClosestValue(root, root, 1, Integer.MAX_VALUE).val);
assertEquals(6, findNodeWithClosestValue(root, root, 6, Integer.MAX_VALUE).val);
assertEquals(13, findNodeWithClosestValue(root, root, 11, Integer.MAX_VALUE).val); // tie b/w 9 and 13
/*
BST looks like:
9
/ \
7 13
/ \ / \
5 8 13 20
*/
root = new TreeNode(9);
root.left = new TreeNode(7);
root.right = new TreeNode(13);
root.left.left = new TreeNode(5);
root.left.right = new TreeNode(8);
root.right.left = new TreeNode(13);
root.right.right = new TreeNode(20);
assertEquals(13, findNodeWithClosestValue(root, root, 15, Integer.MAX_VALUE).val);
}
}