Distance Between Two Nodes in a Binary Tree

Given a binary tree and the values of two distinct nodes, determine the distance between these two nodes in the tree. The distance is defined as the minimum number of edges in the path connecting the two nodes.

Input:

The first line of the input contains an integer ‘T’, which indicates the number of test cases. Each test case consists of the following lines:
- The first line provides the tree's node values in level order, separated by spaces, using ‘-1’ in place of any null nodes. All tree node values are unique.
- The second line contains two integers, Node 1 and Node 2, which represent the values of the nodes whose distance you need to calculate.

Output:

For each test case, output a single integer representing the distance between the two nodes. If either of the nodes is absent from the tree, return -1.

Example:

Input:
1
1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
7 5
Output:
3
Explanation:

The tree structure is as follows:
Level 1: 1
Level 2: 2, 3
Level 3: 4 (left of 2), -1, 5 (left of 3), 6 (right of 3)
Level 4: -1, 7, -1, -1, -1, -1 (7 is right of 4)
Here, the distance between node 7 and node 5 is 3 edges: 7 -> 4, 4 -> 2, 2 -> 1, 1 -> 3, 3 -> 5.

Constraints:

  • 1 <= T <= 102
  • 1 <= N <= 3 * 103
  • 0 <= DATA <= 106 and DATA != -1
Note:

You do not need to print anything. Please ensure to implement the specified function to solve the problem.

AnswerBot
4mo

Calculate the distance between two nodes in a binary tree.

  • Traverse the tree to find the paths from the root to each node

  • Find the lowest common ancestor of the two nodes

  • Calculate the distance by adding...read more

Help your peers!
Select
Add answer anonymously...

JPMorgan Chase & Co. Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Count Subarrays with Given XOR Problem Statement You are given an array of integ...read more
A Software Developer Intern was asked Q. Kth Largest Number Problem Statement You are given a continuous stream of number...read more
A Software Developer Intern was asked Q. Stack using Two Queues Problem Statement Develop a Stack Data Structure to store...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Count Subarrays with Given XOR Problem Statement You are given an array of integ...read more
A Software Developer Intern was asked Q2. Kth Largest Number Problem Statement You are given a continuous stream of number...read more
A Software Developer Intern was asked Q3. Stack using Two Queues Problem Statement Develop a Stack Data Structure to store...read more
JPMorgan Chase & Co. Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits