# 1110. Delete Nodes And Return Forest 删点成林

@TOC

## # 题目描述

Given the root of a binary tree, each node in the tree has a distinct value.

After deleting all nodes with a value in to_delete, we are left with a forest (a disjoint union of trees).

Return the roots of the trees in the remaining forest. You may return the result in any order.

Example 1:

Input: root = [1,2,3,4,5,6,7], to_delete = [3,5]
Output: [[1,2,null,4],[6],[7]]

Constraints:

1. The number of nodes in the given tree is at most 1000.
2. Each node has a distinct value between 1 and 1000.
3. to_delete.length <= 1000
4. to_delete contains distinct values between 1 and 1000.

## # 解题方法

### # 递归

1. 如果该节点是根节点，形成左右两个子树，此时递归左右子树。
2. 如果该节点不是根节点，那么需要修改其父节点指向自己的指针为空，并且递归左右子树。

C++代码如下：

/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
vector<TreeNode*> res;
helper(root, true, res, to_delete);
return res;
}
void helper(TreeNode*& node, bool isRoot, vector<TreeNode*>& res, vector<int>& to_delete) {
if (!node) return;
bool isDel = delCurNode(node, to_delete);
helper(node->left, isDel, res, to_delete);
helper(node->right, isDel, res, to_delete);
if (isRoot && !isDel) {
res.push_back(node);
}
if (!isRoot && isDel) {
node = nullptr;
}
}
bool delCurNode(TreeNode* root, vector<int>& to_delete) {
for (int val : to_delete) {
if (root->val == val) {
return true;
}
}
return false;
}
};

## # 日期

2019 年 9 月 27 日 —— 昨天面快手，竟然是纯刷题