title: A question with Find the closest value in BST
body: <p>I am trying to write a function that takes in a BST and target integer value and returns the closest value to that target value contained in the BST.</p>
  
  <p>However, My code is getting 0 instead of 10 which is answer for this case</p>
  
  <p>can anyone explain underlying concept of my mistake?</p>
  
  <pre><code>    public static int findClosestValueInBst(BST tree, int target) {
          int globalDiff = Integer.MAX_VALUE;
          int answer = 0;
          subFunction(tree, target, globalDiff, answer);
          return answer;
      }
  
  </code></pre>
  
  <pre><code>private static void subFunction (BST tree, int target, int globalDiff, int answer) {
          if (tree == null) {
              return;
          }
          int localDiff = Math.abs(target - tree.value);
          if (localDiff &lt; globalDiff) {
              globalDiff = localDiff;
              answer = tree.value;
          }
          subFunction(tree.left, target, globalDiff, answer);
          subFunction(tree.right, target, globalDiff, answer);
          return;
      }
  
      public static class BST {
          public int value;
          public BST left;
          public BST right;
  
          public BST(int value) {
              this.value = value;
          }
      }
  
      public static void main (String[] args) {
          BST first = new BST(1);
          BST second = new BST(2);
          BST third = new BST(10);
          BST fourth = new BST(5);
          BST fifth = new BST(4);
  
          first.left = second;
          first.right = third;
          second.left = fourth;
          second.right = fifth;
  
          System.out.println(findClosestValueInBst(first, 11));
  
      }
  </code></pre>