daily pastebin goal
49%
SHARE
TWEET

Untitled

a guest Jan 20th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  public int numberBranchesBetweenNodes(E firstElement, E secondElement, Node<E> node) {
  2.         if (node == null) {
  3.             return -1;
  4.         }
  5.        
  6.         if (firstElement.compareTo(node.getElement()) < 0 && secondElement.compareTo(node.getElement()) < 0) {
  7.             return numberBranchesBetweenNodes(firstElement, secondElement, node.getRight());
  8.         }
  9.         if (firstElement.compareTo(node.getElement()) > 0 && secondElement.compareTo(node.getElement()) > 0) {
  10.             return numberBranchesBetweenNodes(firstElement, secondElement, node.getLeft());
  11.         }
  12.        
  13.         int lcaNodeHeight = depth(node.getElement());
  14.         int firstNodeHeight = depth(firstElement);
  15.         int secondNodeHeight = depth(secondElement);
  16.        
  17.         int result = (firstNodeHeight - lcaNodeHeight) + (secondNodeHeight - lcaNodeHeight);
  18.         return result;
  19.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top