Remove Siblings of Node

Given a serializable nested tree and a target id, return a new tree in which every sibling of the target node is removed. Only the path from the root to the target remains at each level. The target and all of its descendants remain unchanged.

If the target is the root or cannot be found, return a deep copy of the input tree unchanged. Do not mutate the input and do not use the DOM.

Examples
Input: [{"id":"a","children":[{"id":"b"},{"id":"c"}]},"b"]
Output: {"id":"a","children":[{"id":"b"}]}
Hints

Remove Siblings of Node

Keep only the root to target path in a nested tree while preserving the target subtree.