FastPrepBinary Tree Nodes at Distance K
Problems/Binary Tree Nodes at Distance K

Binary Tree Nodes at Distance K

Apple logoApple● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Apple full-time onsite interview. The report asks for the nodes exactly k edges from a target. It stresses node identity, because values can repeat. Here every node gets a stable integer ID, and the returned IDs are sorted so answers can be checked.

You are given a binary tree in nodes. Every row has the form [id, value, leftId, rightId], where a child ID of -1 means null. The first row is the root. Node IDs are unique, but node values do not have to be.

Given targetId and a nonnegative distance k, return the IDs of all nodes exactly k edges from the target. Tree edges can be traversed in either direction, so a result can lie below the target, above it, or in another branch. Return IDs in ascending order. If k exceeds every reachable distance, return an empty array.

Function

nodesAtDistanceK(nodes: int[][], targetId: int, k: int) → int[]

Examples

Example 1

nodes = [[1,3,2,3],[2,5,4,5],[3,5,-1,6],[4,6,-1,-1],[5,2,7,8],[6,1,-1,-1],[7,7,-1,-1],[8,4,-1,-1]]targetId = 2k = 2return = [3,7,8]

From node ID 2, node IDs 3, 7, and 8 are exactly two edges away.

Example 2

nodes = [[10,9,20,-1],[20,9,-1,-1]]targetId = 20k = 0return = [20]

With a distance of zero, only the target itself counts. The target's parent has the same value, but it is a separate node one edge away, so it is left out. The answer holds only the target's ID.

Example 3

nodes = [[4,1,-1,-1]]targetId = 4k = 3return = []

The tree has only one node, and that node is the target. No node is three edges away from it, so the answer is an empty array.

Constraints

  • 1 <= nodes.length <= 100000.
  • Every row contains exactly [id, value, leftId, rightId].
  • Node IDs are distinct nonnegative 32-bit integers; child IDs are -1 or reference another row.
  • Values are signed 32-bit integers and may repeat.
  • The rows describe one valid connected binary tree, and the first row is its root.
  • targetId is present in the tree.
  • 0 <= k <= 100000.
  • Return matching node IDs in ascending order.

More Apple Onsite Interview questions

  • Topological OrderingPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • FIFO Mutex Operation TraceONSITE INTERVIEW · Seen Oct 2026
  • Implement Hit CounterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Parse Nested HTML into a TreeONSITE INTERVIEW · Seen Oct 2026
  • Thread-Safe Concurrent Linked ListONSITE INTERVIEW · Seen Sep 2026
  • Palindrome Match PercentageONSITE INTERVIEW · Seen Sep 2026
  • BST to Sorted Circular Doubly Linked ListONSITE INTERVIEW · Seen Aug 2026
  • Classify Natural-Month Distance Between DatesONSITE INTERVIEW · Seen Aug 2026

Similar questions at other companies

  • Distance Between Two Tree NodesAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Find the Tree DiameterAdyen · ONSITE INTERVIEWAdyen interview questions
  • Alternating-Color Binary Tree RootsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Binary Tree Nodes at Distance KGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Base Unit ConversionsAdobe · OAAdobe interview questions
  • Tree Pythagorean TriplesIBM · OAIBM interview questions
  • Tree DiameterQualcomm · PHONE SCREENQualcomm interview questions
  • Maximum Data Transfer TimeMicrosoft · OAMicrosoft interview questions
  • Frog Position After T SecondsInMobi · PHONE SCREENInMobi interview questions

Apple interview experiences

  • Apple SWE Phone Screen: Subarray Products: 1 Experience
  • Apple New Grad SWE Java, Coding and Manager Rounds: 1 Experience
  • Apple AI Coding and Related Interviews: 17 Experiences
  • Apple India SWE New Grad Interviews: 5 Experiences
  • Apple New Grad SWE Phone Screens: 12 Experiences (Part 3 of 3)
  • Apple New Grad SWE and Embedded: 16 Experiences (Part 2 of 3)
All Apple interview questionsSee Apple hiring insights
Saved
class Solution {
    public int[] nodesAtDistanceK(int[][] nodes, int targetId, int k) {
        // Write your code here.
    }
}
Checking account…