Tap here to switch tabs
Problem
Submissions

Problem: Find Distance in a Binary Tree

Try to solve the Find Distance in a Binary Tree problem.

Statement

Given the root of a binary tree and two integers p and q, return the distance between the node with value p and the node with value q.

The distance between two nodes is defined as the number of edges on the path between them.

Constraints:

  • The number of nodes in the tree is in the range [1,104][1, 10^4].

  • 0≤0 \leq Node.val ≤109\leq 10^9

  • All Node.val are unique.

  • p and q are values in the tree.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Find Distance in a Binary Tree

Try to solve the Find Distance in a Binary Tree problem.

Statement

Given the root of a binary tree and two integers p and q, return the distance between the node with value p and the node with value q.

The distance between two nodes is defined as the number of edges on the path between them.

Constraints:

  • The number of nodes in the tree is in the range [1,104][1, 10^4].

  • 0≤0 \leq Node.val ≤109\leq 10^9

  • All Node.val are unique.

  • p and q are values in the tree.