Given the
root
node of a binary tree
,
a target node
target
, and an integer value
K
. Write a program to return a list of the values of all nodes that are at a distance
K
from the
target
node.
Problem Note
- The answer can be returned in any order.
- The given tree is non-empty.
- Each node in the tree has unique values
-
The
target
node is a node in the tree.
Given the following binary tree: root = [5, 6, 3, 1, 7, 9, 4, null, null, 2, 0]
Example 1
Input: root = [5, 6, 3, 1, 7, 9, 4, null, null, 2, 0], target = 6, K = 2
Output: [2, 0, 3]
Explanation: The nodes that are a distance 2 from the target node with value 6 have values 2, 0, and 3.