File tree Expand file tree Collapse file tree 1 file changed +0
-22
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +0
-22
lines changed Original file line number Diff line number Diff line change 6
6
import java .util .List ;
7
7
import java .util .Stack ;
8
8
9
- /**
10
- * 272. Closest Binary Search Tree Value II
11
- *
12
- * Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.
13
-
14
- Note:
15
- Given target value is a floating point.
16
- You may assume k is always valid, that is: k ≤ total nodes.
17
- You are guaranteed to have only one unique set of k values in the BST that are closest to the target.
18
- Follow up:
19
- Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)?
20
-
21
- Hint:
22
-
23
- Consider implement these two helper functions:
24
- getPredecessor(N), which returns the next smaller node to N.
25
- getSuccessor(N), which returns the next larger node to N.
26
- Try to assume that each node has a parent pointer, it makes the problem much easier.
27
- Without parent pointer we just need to keep track of the path from the root to the current node using a stack.
28
- You would need two stacks to track the path in finding predecessor and successor node separately.
29
-
30
- */
31
9
public class _272 {
32
10
33
11
public static class Solution1 {
You can’t perform that action at this time.
0 commit comments