Find distance between 2 nodes in a Binary tree
Pattern: Lowest common ancestor
The path between two nodes goes up from one to their lowest common ancestor and down to the other. Find the LCA, then add the depth of each node below it.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
Return the number of edges between the nodes with values a and b. Values are different and both nodes exist.
[11, 22, 33, 44, 55, 66, 77] with a = 77, b = 22 gives 3. The catalogue has no practice link for this one. Trees in the examples are written in level order, with null for a missing child, as on LeetCode.
Think first
- Where does the path between a and b turn?
- How long is each part?
- Which earlier problem finds the turning point?
Hints
Brute force, and why it fails
Record both root paths, drop the shared start, and add the rest: also O(n).
The key insight
The LCA splits the path into two downward parts, each a simple depth search.
Dry run
| lca | 11 |
|---|
Tree [11, 22, 33, 44, 55, 66, 77], a = 77, b = 22. The LCA is 11.
The plan
m = lca(root, a, b)
return depth(m, a) + depth(m, b)
Complexity
- Time
O(n)- Space
O(h)
Three walks.
Edge cases
- a equals b: 0.
- a is an ancestor of b.
- Siblings: 2.
I've built my logic, show the code
Java
class Solution {
public int findDist(TreeNode root, int a, int b) {
TreeNode meet = lca(root, a, b);
return depthOf(meet, a, 0) + depthOf(meet, b, 0); // down from where the two paths meet
}
private TreeNode lca(TreeNode n, int a, int b) {
if (n == null || n.val == a || n.val == b) return n;
TreeNode l = lca(n.left, a, b), r = lca(n.right, a, b);
if (l != null && r != null) return n;
return l != null ? l : r;
}
private int depthOf(TreeNode n, int v, int d) { // edges from n down to v, or -1
if (n == null) return -1;
if (n.val == v) return d;
int l = depthOf(n.left, v, d + 1);
return l != -1 ? l : depthOf(n.right, v, d + 1);
}
}C++
class Solution {
public:
int findDist(TreeNode* root, int a, int b) {
TreeNode* meet = lca(root, a, b);
return depthOf(meet, a, 0) + depthOf(meet, b, 0); // down from where the two paths meet
}
TreeNode* lca(TreeNode* n, int a, int b) {
if (n == NULL || n->val == a || n->val == b) return n;
TreeNode* l = lca(n->left, a, b);
TreeNode* r = lca(n->right, a, b);
if (l != NULL && r != NULL) return n;
return l != NULL ? l : r;
}
int depthOf(TreeNode* n, int v, int d) { // edges from n down to v, or -1
if (n == NULL) return -1;
if (n->val == v) return d;
int l = depthOf(n->left, v, d + 1);
return l != -1 ? l : depthOf(n->right, v, d + 1);
}
};Python
class Solution:
def findDist(self, root, a: int, b: int) -> int:
def lca(n):
if n is None or n.val in (a, b):
return n
l, r = lca(n.left), lca(n.right)
return n if l and r else l or r
def depth_of(n, v, d) -> int: # edges from n down to v, or -1
if n is None:
return -1
if n.val == v:
return d
l = depth_of(n.left, v, d + 1)
return l if l != -1 else depth_of(n.right, v, d + 1)
meet = lca(root)
return depth_of(meet, a, 0) + depth_of(meet, b, 0) # down from where the two paths meetCommon mistakes
- Adding the depths from the root without subtracting the shared part.
- Searching for the LCA by value when values repeat.