Given a Binary tree, the task is to find the distance between two keys in a binary tree, no parent pointers are given. The distance between two nodes is the minimum number of edges to be traversed to reach one node from another. The given two nodes are guaranteed to be in the binary tree and all node values are unique.
Using LCA and Path Length - O(n) Time and O(h) Space
The idea to first find the Lowest Common Ancestor (LCA) of two given nodes in a binary tree. Once the LCA is found, we calculate the distance between the two target nodes by finding the path length from the root to each node and then subtracting twice the path length from the root to the LCA.
Start by traversing the tree to find the level of the first target node. If the node is found, return its level; otherwise, continue searching recursively in the left and right subtrees.
Recursively check each node to see if it matches either of the target nodes. If a match is found, store the current level. If both nodes are located in different subtrees, calculate the distance based on their levels.
If only one of the target nodes is found, determine the distance to the other node using the level obtained from the LCA. This involves further traversals to find the level of the remaining target node.
Using the levels obtained, compute the total distance between the two nodes by adding their levels and subtracting twice the level of the LCA. This gives the direct distance between the two target nodes.
Finally, return the calculated distance.
Below is the implementation of the above approach.
Output
4
Using LCA - O(n) Time and O(h) Space
The idea is to first identify their Lowest Common Ancestor (LCA). Once the LCA is determined, the distance between each node and the LCA is calculated. The sum of these distances gives the total distance between the two nodes
Step-By-Step Implementation :
Start at the root of the tree and recursively traverse the left and right subtrees. If the current node is null, return null. If the current node matches either of the two nodes, return the current node. If both left and right subtree calls return non-null values, the current node is the LCA.
From the LCA node, find the level of the first node. If the current node matches the target, return the current depth. Recursively check both the left and right children, increasing the depth by one until the node is found.
Perform the same level calculation for the second node. Collect the depth values for both nodes, which represent the distance from the LCA to each of the two nodes.
Sum the distances obtained from the previous steps. This total represents the distance between the two nodes in the binary tree, giving the final result.
Below is the implementation of the above approach.
Output
4
Using LCA (one pass) - O(n) Time and O(h) Space
We first find the LCA of two nodes. Then we find the distance from LCA to two nodes. We know that distance between two node(let suppose n1 and n2) = distance between LCA and n1 + distance between LCA and n2.
Step-By-Step Implementation :
Recursively traverses the tree to find the distance between the two target nodes. During traversal, check if the current node is one of the target nodes.
While traversing, if one of the target nodes is found, check if there are any descendants of the other target node. This will help in determining whether to continue counting the distance or to reset it.
If both target nodes are found in the left and right subtrees of the current node, that node is their LCA. At this point, calculate the distance from the LCA to each target node.
After calculating the distances from the LCA to both target nodes, return their sum as the final distance between the two nodes in the binary tree.
Below is the implementation of the above approach.