Advertisement
Closest Neighbour in BST
JavaView on GFG
Closest Neighbour in BST.java
Java
class Solution {
public int findMaxFork(Node root, int k) {
int ans = -1;
while (root != null) {
if (root.data == k)
return k;
else if (root.data < k) {
ans = root.data;
root = root.right;
} else
root = root.left;
}
return ans;
}
}Advertisement
Was this solution helpful?