Node* lowestCommonAncestor(Node* root, int a, int b) {
if (!root || root->data == a || root->data == b)
Node* leftLCA = lowestCommonAncestor(root->left, a, b);
Node* rightLCA = lowestCommonAncestor(root->right, a, b);
return leftLCA ? leftLCA : rightLCA;
int distanceFromNode(Node* root, int target, int distance) {
if (root->data == target)
int leftDistance = distanceFromNode(root->left, target, distance + 1);
return distanceFromNode(root->right, target, distance + 1);
int findDist(Node* root, int a, int b) {
Node* lca = lowestCommonAncestor(root, a, b);
int distanceToA = distanceFromNode(lca, a, 0);
int distanceToB = distanceFromNode(lca, b, 0);
return distanceToA + distanceToB;