Advertisement
Guest User

Untitled

a guest
Jan 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement