Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5715 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 1115 |

@bt8816103042 | 739 |

@SherlockHolmes3 | 447 |

@codeantik | 441 |

@SHOAIBVIJAPURE | 430 |

@shalinibhataniya1097 | 408 |

@ShamaKhan1 | 392 |

@neverevergiveup | 381 |

@amrutakashikar2 | 355 |

@mahlawatep | 353 |

@murarry3625 | 352 |

Complete Leaderboard |

Given a **BST** and an integer. Find the least absolute difference between any node value of the BST and the given integer.

**Example 1:**

**Input:
** 10
/ \
2 11
/ \
1 5
/ \
3 6
\
4
K = 13
**Output: **2**
Explanation: **K=13. The node that has
value nearest to K is 11. so the answer
is 2

**Example 2:**

**Input:
** 8
/ \
1 9
\ \
4 10
/
3
K = 9
**Output: **0**
Explanation: **K=9. The node that has
value nearest to K is 9. so the answer
is 0.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function** minDiff() **that takes the root of the BST and an integer K as its input and returns the minimum absolute difference between any node value of the BST and the integer K.

**Expected Time Complexity: **O(Height of the BST).

**Expected Auxiliary Space: **O(Height of the BST).

**Constraints:**

1 <= Number of nodes <= 100000

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Find the Closest Element in BST

...