PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Ripple

Find path between two nodes in a binary tree

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of tree data structures, traversal and path reconstruction techniques, including reasoning about lowest common ancestors, ancestor–descendant relationships, duplicate identification, and handling absent targets.

  • hard
  • Ripple
  • Coding & Algorithms
  • Software Engineer

Find path between two nodes in a binary tree

Company: Ripple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

## Problem Given a binary tree and two target nodes `a` and `b`, return the **simple path** from `a` to `b` (inclusive) as an ordered list of node values. - The path should list nodes in the order you would visit them when traveling from `a` to `b` along parent/child links. - You may assume the tree nodes have fields like `val`, `left`, and `right`. ### Example 1 Tree: ``` 1 / \ 2 3 ``` Input: `a = 2`, `b = 3` Output: `[2, 1, 3]` ### Example 2 Tree: ``` 1 / \ 2 3 / \ 6 5 / \ 7 8 ``` Input: `a = 3`, `b = 5` Output: `[3, 1, 2, 5]` ## Requirements - Define what your function receives for `a` and `b` (e.g., node references/pointers, unique IDs, or values). - Handle cases where one node is the ancestor of the other. - Clarify behavior if one or both targets are not present (e.g., return empty list or error). ## Follow-ups 1. **Duplicate values:** If the tree can contain duplicate values, how does your approach change? What should be used to identify `a` and `b`? 2. **BST variant:** If the tree is a Binary Search Tree, what (if anything) changes in: - the algorithm, - and the time complexity (compared to a general binary tree)?

Quick Answer: This question evaluates understanding of tree data structures, traversal and path reconstruction techniques, including reasoning about lowest common ancestors, ancestor–descendant relationships, duplicate identification, and handling absent targets.

Ripple logo
Ripple
Nov 10, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
4
0

Problem

Given a binary tree and two target nodes a and b, return the simple path from a to b (inclusive) as an ordered list of node values.

  • The path should list nodes in the order you would visit them when traveling from a to b along parent/child links.
  • You may assume the tree nodes have fields like val , left , and right .

Example 1

Tree:

    1
   / \
  2   3

Input: a = 2, b = 3 Output: [2, 1, 3]

Example 2

Tree:

      1
     / \
    2   3
   / \
  6   5
 / \
7   8

Input: a = 3, b = 5 Output: [3, 1, 2, 5]

Requirements

  • Define what your function receives for a and b (e.g., node references/pointers, unique IDs, or values).
  • Handle cases where one node is the ancestor of the other.
  • Clarify behavior if one or both targets are not present (e.g., return empty list or error).

Follow-ups

  1. Duplicate values: If the tree can contain duplicate values, how does your approach change? What should be used to identify a and b ?
  2. BST variant: If the tree is a Binary Search Tree, what (if anything) changes in:
    • the algorithm,
    • and the time complexity (compared to a general binary tree)?

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Ripple•More Software Engineer•Ripple Software Engineer•Ripple Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.