Advertisement
sweet1cris

Untitled

Sep 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1. public class LargestNumberSmallerBST {
  2.   public int largestSmaller(TreeNode root, int target) {
  3.     // Assumptions: the binary search tree is not null.
  4.     int result = Integer.MIN_VALUE;
  5.     while (root != null) {
  6.       if (root.key >= target) {
  7.         root = root.left;
  8.       } else {
  9.         // the candidates are all the nodes on the path of
  10.         // searching for target, which is smaller than target.
  11.         // and notice that, the later searched node has lager
  12.         // value than the earlier searched ones.
  13.         result = root.key;
  14.  
  15.         root = root.right;
  16.       }
  17.     }
  18.     return result;
  19.   }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement