As reported: Goldman Sachs Associate Superday, DSA round 1 (2026), third question: a variation where, for any given node of a tree, you find all nodes at distance
k— which requires moving upward through parents too.
Given the root of a binary tree, a target node and an integer k, return the values of all nodes exactly k edges away from target, in any order.
This is LeetCode 863 — All Nodes Distance K in Binary Tree.
Example 1
root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
-> [1,4,7]
Example 2
root = [1], target = 1, k = 3
-> []
Constraints: 1 ≤ nodes ≤ 500; values are unique; 0 ≤ k ≤ 1000.
Code runner format: one JSON value per line: root, then target, then k (LeetCode's test-case format). Implement distanceK(root, target, k); the harness prints the result as JSON (any order is accepted).