To find largest element smaller than K in a BST

前端 未结 5 1070
梦如初夏
梦如初夏 2020-12-25 14:11

Given a binary search tree and an integer K, i would like to find the largest element less than K.

In the below tree,

for K = 13, result = 12
for K =         


        
5条回答
  •  悲&欢浪女
    2020-12-25 14:41

    I suggest that you walk through the code in your local implementation of set::upper_bound for guidance. This is not the solution to your exact problem, but very close.

    In general in real life, most of these problems do not need to be solved in your own code. STL can do many common tasks for you. It's useful to know how to solve them of course, hence the test.

提交回复
热议问题