FastPrepAll Nodes Distance K in a Binary Tree
Problems/All Nodes Distance K in a Binary Tree

All Nodes Distance K in a Binary Tree

Amazon logoAmazon● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: An Amazon full-time phone screen report names All Nodes Distance K in a Binary Tree directly. Our version is about 98% the same as the reported question.

You are given the root of a binary tree whose node values are unique, an integer target equal to one node value, and a nonnegative integer k.

Two directly connected tree nodes are one edge apart. Return the values of every node whose shortest-path distance from the target node is exactly k.

Return the values in ascending order. If no node is exactly k edges away, return an empty list.

Function

distanceK(root: TreeNode, target: int, k: int) → List<Integer>

Examples

Example 1

root = [3,5,1,6,2,0,8,null,null,7,4]target = 5k = 2return = [1,4,7]
root
356274108

From node 5, nodes 7 and 4 are reached through node 2, while node 1 is reached through node 3. Each is two edges away.

Example 2

root = [1]target = 1k = 0return = [1]
root
1

The target node itself is the only node at distance 0.

Constraints

  • The tree contains between 1 and 500 nodes.
  • 0 <= Node.val <= 500.
  • Every node value is unique.
  • target is the value of a node in the tree.
  • 0 <= k <= 1000.

More Amazon Phone Screen questions

  • Number of IslandsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 3SumPHONE SCREEN · Seen Nov 2026
  • Circular Primes Below a LimitPHONE SCREEN · 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
  • LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Two SumOA · PHONE SCREEN · Seen Nov 2026
  • Count the 1 Bits in a 32-Bit ValuePHONE SCREEN · Seen Nov 2026

Similar questions at other companies

  • Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
  • Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions
  • Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Maximum Data Transfer TimeMicrosoft · OAMicrosoft interview questions
  • Longest Transfer Time in a Server TreeRippling · OARippling interview questions
  • Special Diameter EndpointsAgoda · OAAgoda interview questions
  • Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
  • Evaluate DivisionApple · PHONE SCREENApple interview questions
  • Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
  • Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake 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 List<Integer> distanceK(TreeNode root, int target, int k) {
        // Write your solution here.
    }
}
Checking account…