impl Solution {
pub fn del_nodes(root: Option<Rc<RefCell<TreeNode>>>, to_delete: Vec<i32>) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
fn delete_nodes(root: Option<Rc<RefCell<TreeNode>>>, to_delete: &std::collections::HashSet<i32>, forest: &mut Vec<Option<Rc<RefCell<TreeNode>>>>) -> Option<Rc<RefCell<TreeNode>>> {
if let Some(ref node) = root {
let mut node = node.as_ref().borrow_mut();
(node.left, node.right) = (
delete_nodes(node.left.clone(), to_delete, forest),
delete_nodes(node.right.clone(), to_delete, forest),
);
if to_delete.contains(&node.val) {
if node.left.is_some() {
forest.push(node.left.clone());
}
if node.right.is_some() {
forest.push(node.right.clone());
}
return None
}
return root.clone()
}
None
}
let (mut to_delete, mut forest) = (to_delete.into_iter().collect(), Vec::new());
if delete_nodes(root.clone(), &to_delete, &mut forest).is_some() {
forest.push(root)
}
forest
}
}