Number of comparisons will be O(logn) and additions will be constant.
Algorithm:
Step 1: First find out the lowest common ancestor of a and b.
https://afteracademy.com/blog/lowest-common-ancestor-of-a-binary-tree
O(logn)
Step 2: then find out the parent of both a and b using given algorithm
https://www.geeksforgeeks.org/find-the-parent-of-a-node-in-the-given-binary-tree/
O(logn)
Step 3: then use the given logic for finding out the number of additions required.
N(p) : denotes no of children in its subtree ( it is stored in each node)
P(p) : denotes parent of node p
Z = no of nodes between [a,b]
root is the lowest common ancestor of a and b.
case 1 : if a is left child of its parent and b is right child of its parent
Z= N(root) - ( N(a->left) + 1) - ( N(b->right) +1 ) +1
case 2: a is right and b is right child of their parent
Z = N(root) - ( N(a->left) + 1) - ( N( P(a) -> left ) +1 ) - ( N(b->right) +1 ) +1
case 3 : if a is left child of its parent and b is left child of its parent
Z= N(root) - ( N(a->left) + 1) - ( N(b->right) +1 ) - ( N ( P(b)->right ) +1) +1
case 4: a is right and b is left child of their parent
Z = N(root) - ( N(a->left) + 1) - ( N( P(a) -> left ) +1 ) - ( N(b->right) +1 ) -
( N ( P(b)->right ) +1)+1
So only a constant no of additions required.