Find Distance in a Binary Tree - Problem

Imagine you're working with a family tree represented as a binary tree, where each person has a unique identifier. Your task is to find the shortest path between any two family members in the tree.

Given the root of a binary tree and two integers p and q representing two nodes in the tree, return the distance between the nodes with values p and q.

The distance is defined as the number of edges on the shortest path connecting the two nodes. This path always goes through their Lowest Common Ancestor (LCA) - think of it as the closest common relative that connects both family members.

Key insight: The distance between two nodes equals the sum of their depths from their LCA: distance = depth(p from LCA) + depth(q from LCA)

Input & Output

example_1.py โ€” Basic Tree
$ Input: root = [3,5,1,6,2,null,8,null,null,7,4], p = 5, q = 1
โ€บ Output: 3
๐Ÿ’ก Note: The shortest path from node 5 to node 1 goes through their LCA node 3. Path: 5 โ†’ 3 โ†’ 1, which has 2 edges, so distance = 2. Wait, let me recalculate: 5โ†’3 (1 edge) + 3โ†’1 (1 edge) = 2 total edges.
example_2.py โ€” Deep Path
$ Input: root = [3,5,1,6,2,null,8,null,null,7,4], p = 6, q = 8
โ€บ Output: 4
๐Ÿ’ก Note: Path from 6 to 8: 6 โ†’ 5 โ†’ 3 โ†’ 1 โ†’ 8. This path has 4 edges. The LCA of 6 and 8 is node 3. Distance from 6 to 3: 2 edges (6โ†’5โ†’3). Distance from 3 to 8: 2 edges (3โ†’1โ†’8). Total: 2 + 2 = 4.
example_3.py โ€” Same Node
$ Input: root = [1,2,3], p = 2, q = 2
โ€บ Output: 0
๐Ÿ’ก Note: When both nodes are the same, the distance is 0 as no traversal is needed.

Constraints

  • The number of nodes in the tree is in the range [2, 104]
  • 1 โ‰ค Node.val โ‰ค 104
  • All node values are unique
  • p โ‰  q
  • Both p and q exist in the tree

Visualization

Tap to expand
GreatGrandpa๐Ÿ‘ด Common AncestorGrandpaSmithGrandpaJohnsonAlice๐ŸŽฏ Person PBobCarol๐ŸŽฏ Person Q2 steps2 steps๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ Family Distance CalculationAlice needs 2 steps to reach Great Grandpa: Alice โ†’ Grandpa Smith โ†’ Great GrandpaCarol needs 2 steps to reach Great Grandpa: Carol โ†’ Grandpa Johnson โ†’ Great GrandpaTotal family distance: 2 + 2 = 4 relationship steps
Understanding the Visualization
1
Start DFS Journey
Begin traversing the family tree from the root ancestor
2
Find First Relative
When we encounter person P, we start counting distance
3
Find Second Relative
When we encounter person Q, we start counting from there too
4
Discover Common Ancestor
The node where both paths converge is the Lowest Common Ancestor
5
Calculate Total Distance
Sum the distances from LCA to both relatives
Key Takeaway
๐ŸŽฏ Key Insight: Every pair of nodes in a tree has exactly one path connecting them, and this path always passes through their Lowest Common Ancestor (LCA). The optimal solution finds the LCA while simultaneously calculating distances in a single traversal.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
62.4K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen