All Nodes Distance K in Binary Tree
Given the root of a binary tree, a target node, and an integer k, return the values of all nodes that are exactly distance k from the target, where distance is the number of edges on the path between two nodes. The answer may be returned in any order.
Open official problem prompt ↗List every node whose shortest path to the target is exactly k edges, counting movement up and down the tree.
Dropping a pebble at the target node in a pond of connected rooms; the ripple expands one room per step, and you note every room the ripple reaches on step k.
- Input
- root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
- Output
- [7,4,1]
- Why
- From node 5, nodes 7 and 4 are two edges down, and node 1 is two edges up-and-over through the root 3.
The number of nodes is in the range [1, 500]0 <= Node.val <= 500All Node.val are uniquetarget is guaranteed to be in the tree0 <= k <= 1000