FastPrepRoot-to-Node Path in a Binary Tree
Problems/Root-to-Node Path in a Binary Tree

Root-to-Node Path in a Binary Tree

Amazon logoAmazon● EasyFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Amazon full-time onsite interview. The report names a task of finding the path from the root to a node, but does not say what kind of tree it is, what the function looks like, or what to do when the target is missing. Our version uses a binary tree and returns an empty path when the target is not there. It is about 88% the same as the reported question.

Given the root of a binary tree whose node values are unique and an integer target, return the node values along the path from the root to the node whose value equals target, including both endpoints.

If the target does not appear in the tree, return an empty array.

Function

rootToNodePath(root: TreeNode, target: int) → int[]

Examples

Example 1

root = [1,2,3,4,5,null,6]target = 5return = [1,2,5]
root
124536

Starting at 1, move left to 2 and then right to 5.

Example 2

root = [7]target = 7return = [7]
root
7

The root is the target, so the path contains one value.

Example 3

root = [1,2,3]target = 9return = []
root
123

No node has value 9.

Constraints

  • The tree contains from 0 through 10^5 nodes.
  • -10^9 <= Node.val, target <= 10^9.
  • All node values are unique.

More Amazon Onsite Interview questions

  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Cheapest Flights Within K StopsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Course Schedule IIPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Dungeon GameONSITE INTERVIEW · Seen Nov 2026
  • Integer BreakONSITE INTERVIEW · Seen Nov 2026
  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • All Root-to-Leaf Paths in a Binary TreeGoogle · ONSITE INTERVIEWGoogle interview questions
  • Root-to-Leaf Paths with a Target SumBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
  • Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
  • Maximum Depth of Binary TreeLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
  • Path SumSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Binary Tree Preorder TraversalAnduril · ONSITE INTERVIEWAnduril interview questions
  • Symmetric Binary TreeFigma · ONSITE INTERVIEWFigma interview questions
  • Diameter of a Binary TreeMeta · ONSITE INTERVIEWMeta interview questions
  • Check That Every Node Equals Its Descendant SumMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Mirror Image Binary TreesExpedia · ONSITE INTERVIEWExpedia interview questions

Amazon interview experiences

  • Amazon SWE OA with AI Coding: 1 Experience
  • Amazon SWE Intern Phone Screen Interview Experience: 1 Experience
  • Amazon New Grad SDE I OA and Virtual Onsite: 1 Experience
  • Amazon BIE Intern SQL and Interview Questions: 17 Experiences
  • Amazon Experienced SWE AI-Assisted Debugging OA: 1 Experience
  • Amazon SDE II (L5) Interview Questions: 5 Experiences
See Amazon hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int[] rootToNodePath(TreeNode root, int target) {
      // write your code here
    }
}
Checking account…