Binary Tree Nodes at Distance K
Updated
Problem statement
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]Distance zero contains the target itself. Equal values do not merge the two node identities.
Example 3
nodes = [[4,1,-1,-1]]targetId = 4k = 3return = []No node is three edges from the only node in the tree.
Constraints
1 <= nodes.length <= 100000.- Every row contains exactly
[id, value, leftId, rightId]. - Node IDs are distinct nonnegative 32-bit integers; child IDs are
-1or 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.
targetIdis present in the tree.0 <= k <= 100000.- Return matching node IDs in ascending order.
More Goldman Sachs Onsite Interview questions
- Knight Probability in ChessboardONSITE INTERVIEW · Seen Nov 2026
- Best Average Student ScorePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Meeting RoomsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Meeting Rooms Without a Sweep LineONSITE INTERVIEW · Seen Oct 2026
- First Non-Repeating CharacterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Root of the Largest TreePHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Dungeon HealthONSITE INTERVIEW · Seen Sep 2026
- Insert, Delete, And Get Random In Constant TimeONSITE INTERVIEW · Seen Sep 2026
- Least Recently Used CacheONSITE INTERVIEW · Seen Sep 2026
- Staircase Search In A Sorted MatrixONSITE INTERVIEW · Seen Sep 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
- Binary Tree Nodes at Distance KApple · ONSITE INTERVIEWApple interview questions
- Find the Tree DiameterAdyen · ONSITE INTERVIEWAdyen interview questions
- Alternating-Color Binary Tree RootsGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle 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