Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int numberBranchesBetweenNodes(E firstElement, E secondElement, Node<E> node) {
- if (node == null) {
- return -1;
- }
- if (firstElement.compareTo(node.getElement()) < 0 && secondElement.compareTo(node.getElement()) < 0) {
- return numberBranchesBetweenNodes(firstElement, secondElement, node.getRight());
- }
- if (firstElement.compareTo(node.getElement()) > 0 && secondElement.compareTo(node.getElement()) > 0) {
- return numberBranchesBetweenNodes(firstElement, secondElement, node.getLeft());
- }
- int lcaNodeHeight = depth(node.getElement());
- int firstNodeHeight = depth(firstElement);
- int secondNodeHeight = depth(secondElement);
- int result = (firstNodeHeight - lcaNodeHeight) + (secondNodeHeight - lcaNodeHeight);
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement