The downward direction is easy enough, just DFS from the target.
Treat the tree as an undirected graph by adding parent pointers, then perform a BFS from the target node to find all nodes at distance K. Alternatively, use a recursive DFS that returns distances from the target to ancestors and explores subtrees. Clearly explain the chosen method and its complexity.
Pro tip: Clarify edge cases upfront (e.g., K=0, target not in tree, K > tree height) and discuss trade-offs between BFS and DFS approaches. Mention that BFS naturally finds nodes at exact distance K and avoids unnecessary traversal.
Ask if the tree is binary, if node values are unique, and what to return if no nodes are at distance K. Confirm whether K can be 0 or larger than the tree height.
Decide between converting to a graph with parent pointers and BFS, or using DFS with distance tracking. Explain why one is more suitable given constraints.
If BFS: build parent map, then BFS from target up to depth K. If DFS: recursively compute distances from target to ancestors and explore downward subtrees.
State time and space complexity. BFS: O(N) time and O(N) space. DFS: O(N) time and O(H) space for recursion stack.
Walk through a small tree example, including edge cases like K=0 and target at leaf. Verify output correctness.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.