Medium · Trees
Find distance of two nodes
Given the root of a binary tree with unique values and two nodes p and q in it, identified by their values, return the number of edges on the path between p and q.
Examples
Example 1
{
"tree": [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4],
"p": 6,
"q": 4
}Output: distance = 3
Example 2
{
"tree": [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4],
"p": 4,
"q": 8
}Output: distance = 5
Rebuild it in the studio
Read every interview problem free. Ten rooms need no account. A token opens a problem in full — Pro never counts.