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

Binary Tree Nodes at Distance K

Morgan Stanley logoMorgan Stanley● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Morgan Stanley onsite interview for a full-time role. The author asks for all elements k edges from a given node. No input format or output order is given. Here IDs are unique and the tree comes as parallel rows of binary child indexes. The root is index 0. Distance counts edges in both directions. The IDs come out sorted. We also chose the limits and what happens when a node is absent or the result is empty.

Our version is about 90% the same as the reported question.

Return all node IDs at exactly k edges from targetId in a binary tree. A path may move from a node to a child or to its parent. Return the IDs in ascending numeric order, without duplicates.

For this exercise, nodeIds[i] is the distinct ID at index i, and children[i] contains exactly two indices: left and right child, with -1 for a missing child. Root index 0 is used when nonempty. Child entries are indices, while targetId is an ID; they are different concepts.

The input is a valid rooted binary tree. For k=0 return the target ID alone if present. If the target is absent, the tree is empty or no node has distance k, return an empty array. Leave every input unchanged.

Function

nodesAtDistanceK(nodeIds: int[], children: int[][], targetId: int, k: int) → int[]

Examples

Example 1

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

From ID 5, two-edge paths reach ID 1 through parent 3, and IDs 7 and 4 through child 2. Sorting gives [1,4,7]. The target itself has distance 0.

Example 2

nodeIds = [900,100,700,300]children = [[1,2],[-1,3],[-1,-1],[-1,-1]]targetId = 100k = 1return = [300,900]

Target ID 100 is at index 1. Its one-edge neighbors have IDs 900 and 300; sorting gives [300,900]. Numeric index 100 is not used as a node index.

Constraints

  • For this exercise, assume 0 <= nodeIds.length <= 200.
  • For this exercise, assume 0 <= children.length <= 200.
  • nodeIds and children have equal length.
  • Every node ID is between 0 and 1000000, inclusive.
  • All node IDs are distinct.
  • Every children row has exactly 2 entries: left child index then right child index.
  • Every child entry is between -1 and 199, inclusive.
  • For nonempty input, root index 0 and the non-null child links form a valid binary tree: each non-root index has exactly one parent, all nodes are reachable, and no child is repeated. Each non-null child index is less than nodeIds.length.
  • For this exercise, assume 0 <= targetId <= 1000000.
  • For this exercise, assume 0 <= k <= 200.

More Morgan Stanley Onsite Interview questions

  • Generate Strings of Length at Least ThreeONSITE INTERVIEW · Seen Sep 2026
  • Maximum Concurrent ProcessesOA · ONSITE INTERVIEW · Seen Sep 2026
  • Kth Largest Array ElementONSITE INTERVIEW · Seen Jul 2026
  • Run-Length Encode Consecutive Letter RunsONSITE INTERVIEW · Seen Jul 2026
  • Maximum Edge Removals for Two TravelersONSITE INTERVIEW · Seen Oct 2025
  • Remove K DigitsONSITE INTERVIEW · Seen Oct 2025
  • Count Ideal ArraysONSITE INTERVIEW · Seen Aug 2023
  • Distinct Palindrome PartitionsONSITE INTERVIEW · Seen Aug 2023
  • LFU Cache OperationsONSITE INTERVIEW · Seen Aug 2023
  • All-Pairs Shortest PathsOA · ONSITE INTERVIEW · Seen Feb 2022

Similar questions at other companies

  • Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
  • Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Zigzag Level-Order TraversalFlipkart · ONSITE INTERVIEW
  • Binary Tree Zigzag Level Order TraversalLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
  • Diameter of an Acyclic Undirected GraphSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Binary Tree Nodes at Distance KApple · ONSITE INTERVIEWApple interview questions
  • Binary Tree Zigzag Level Order TraversalMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
  • Bottom View of a Binary TreeOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
  • Find the Tree DiameterAdyen · ONSITE INTERVIEWAdyen interview questions
  • Top View of a Binary TreeCapillary Technologies · ONSITE INTERVIEWCapillary Technologies interview questions

Interview reports that mention this question

  • Morgan Stanley Apprenticeship and Tech Interviews: 6 Experiences

Morgan Stanley interview experiences

  • Morgan Stanley New Grad Technology Analyst: 2 Experiences
  • Morgan Stanley Intern Oldest Loops: 5 Experiences (Part 5 of 5)
  • Morgan Stanley Intern OOP Loops: 5 Experiences (Part 4 of 5)
  • Morgan Stanley Intern Earlier Loops: 5 Experiences (Part 3 of 5)
  • Morgan Stanley Intern Tech Rounds: 5 Experiences (Part 2 of 5)
All Morgan Stanley interview questionsSee Morgan Stanley hiring insights
Saved
class Solution {
    public int[] nodesAtDistanceK(int[] nodeIds, int[][] children, int targetId, int k) {
        // Write your code here.
    }

}
Checking account…