0

Find the largest number smaller than a given number(n) in the given BST.

I tried this:

start at root node, check if root equal to n-1. If root is less than n-1 recursively call the method on root.right. If root is more than n-1 recursively call the method on root.left.

This solution works if i have a global variable to keep track to current largest element smaller that n but this is not good solution. Please let me know how to solve without global variable

user830818
  • 407
  • 1
  • 7
  • 18

1 Answers1

0

Just put the largest number as a parameter in your method. When you first call it, pass in the lowest number it could possibly be.

cjk
  • 45,739
  • 9
  • 81
  • 112