Find shortest distance in binary tree

Last updated: August 19, 2025

Quick Overview

Given a binary tree, write a function to find the shortest distance between two given nodes in the tree. The function should take the root of the binary tree and the two target nodes as input and return the shortest distance as an integer. If either of the nodes does not exist in the tree, return -1.

Zscaler
Coding & Algorithms
Machine Learning Engineer
Zscaler
August 19, 2025
Machine Learning Engineer
Phone Screen
Coding & Algorithms
Medium

25

8

1,668 solved


Given a binary tree, write a function to find the shortest distance between two given nodes in the tree. The function should take the root of the binary tree and the two target nodes as input and return the shortest distance as an integer. If either of the nodes does not exist in the tree, return -1.

Coding interviews at Zscaler focus on problem-solving approach as much as the final solution. The interviewer wants to see you break down the problem, consider edge cases, and optimize iteratively. Communication throughout the process is key.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Dynamic programming and memoization
Time and space complexity analysis
Binary search and divide and conquer
Hash maps and frequency counting
Edge cases and input validation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What is the worst-case input for your solution?
  • How would you test this solution thoroughly?
  • What if the input doesn't fit in memory?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem requires finding the shortest distance between two nodes in a binary tree. This scenario can be effectively approached using a combination of Depth First Search (DFS) to locate the nod...

Approach
  1. Find the Lowest Common Ancestor (LCA) of the two nodes. The LCA is the deepest node that is an ancestor to both nodes, which minimizes the distance.
  2. **Calculate the distance from the LCA to...

Submit Your Answer
Markdown supported

Related Questions