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"}]}
Input: [{"id":"a","children":[{"id":"b","children":[{"id":"d"},{"id":"e"}]},{"id":"c"}]},"e"]
Output: {"id":"a","children":[{"id":"b","children":[{"id":"e"}]}]}
Hints
First deep-copy a node when it is the target, preserving all of its descendants.
Recursively search each child and keep only the child that reports success.
If no child finds the target, return a deep copy of the original tree.
Remove Siblings of Node
Keep only the root to target path in a nested tree while preserving the target subtree.