comments | difficulty | edit_url | rating | source | tags | |||||
---|---|---|---|---|---|---|---|---|---|---|
true |
中等 |
1511 |
第 144 场周赛 Q3 |
|
给出二叉树的根节点 root
,树上每个节点都有一个不同的值。
如果节点值在 to_delete
中出现,我们就把该节点从树上删去,最后得到一个森林(一些不相交的树构成的集合)。
返回森林中的每棵树。你可以按任意顺序组织答案。
示例 1:
输入:root = [1,2,3,4,5,6,7], to_delete = [3,5] 输出:[[1,2,null,4],[6],[7]]
示例 2:
输入:root = [1,2,4,null,3], to_delete = [3] 输出:[[1,2,4]]
提示:
- 树中的节点数最大为
1000
。 - 每个节点都有一个介于
1
到1000
之间的值,且各不相同。 to_delete.length <= 1000
to_delete
包含一些从1
到1000
、各不相同的值。
我们先用哈希表或者一个长度为
接下来,设计一个函数
- 如果
$root$ 为空,那么我们返回空; - 否则,我们递归执行
$dfs(root.left)$ 和$dfs(root.right)$ ,并将返回值分别赋给$root.left$ 和$root.right$ 。如果$root$ 不需要被删除,那么我们返回$root$ ;如果$root$ 需要被删除,那么我们分别判断$root.left$ 和$root.right$ 是否为空,如果它们不为空,那么我们将它们加入答案数组中;最后返回空。
在主函数中,我们调用
时间复杂度
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def delNodes(
self, root: Optional[TreeNode], to_delete: List[int]
) -> List[TreeNode]:
def dfs(root: Optional[TreeNode]) -> Optional[TreeNode]:
if root is None:
return None
root.left, root.right = dfs(root.left), dfs(root.right)
if root.val not in s:
return root
if root.left:
ans.append(root.left)
if root.right:
ans.append(root.right)
return None
s = set(to_delete)
ans = []
if dfs(root):
ans.append(root)
return ans
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private boolean[] s = new boolean[1001];
private List<TreeNode> ans = new ArrayList<>();
public List<TreeNode> delNodes(TreeNode root, int[] to_delete) {
for (int x : to_delete) {
s[x] = true;
}
if (dfs(root) != null) {
ans.add(root);
}
return ans;
}
private TreeNode dfs(TreeNode root) {
if (root == null) {
return null;
}
root.left = dfs(root.left);
root.right = dfs(root.right);
if (!s[root.val]) {
return root;
}
if (root.left != null) {
ans.add(root.left);
}
if (root.right != null) {
ans.add(root.right);
}
return null;
}
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
bool s[1001];
memset(s, 0, sizeof(s));
for (int x : to_delete) {
s[x] = true;
}
vector<TreeNode*> ans;
function<TreeNode*(TreeNode*)> dfs = [&](TreeNode* root) -> TreeNode* {
if (!root) {
return nullptr;
}
root->left = dfs(root->left);
root->right = dfs(root->right);
if (!s[root->val]) {
return root;
}
if (root->left) {
ans.push_back(root->left);
}
if (root->right) {
ans.push_back(root->right);
}
return nullptr;
};
if (dfs(root)) {
ans.push_back(root);
}
return ans;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func delNodes(root *TreeNode, to_delete []int) (ans []*TreeNode) {
s := make([]bool, 1001)
for _, x := range to_delete {
s[x] = true
}
var dfs func(*TreeNode) *TreeNode
dfs = func(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
root.Left = dfs(root.Left)
root.Right = dfs(root.Right)
if !s[root.Val] {
return root
}
if root.Left != nil {
ans = append(ans, root.Left)
}
if root.Right != nil {
ans = append(ans, root.Right)
}
return nil
}
if dfs(root) != nil {
ans = append(ans, root)
}
return
}
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function delNodes(root: TreeNode | null, to_delete: number[]): Array<TreeNode | null> {
const s: boolean[] = Array(1001).fill(false);
for (const x of to_delete) {
s[x] = true;
}
const ans: Array<TreeNode | null> = [];
const dfs = (root: TreeNode | null): TreeNode | null => {
if (!root) {
return null;
}
root.left = dfs(root.left);
root.right = dfs(root.right);
if (!s[root.val]) {
return root;
}
if (root.left) {
ans.push(root.left);
}
if (root.right) {
ans.push(root.right);
}
return null;
};
if (dfs(root)) {
ans.push(root);
}
return ans;
}
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number[]} to_delete
* @return {TreeNode[]}
*/
var delNodes = function (root, to_delete) {
const s = Array(1001).fill(false);
for (const x of to_delete) {
s[x] = true;
}
const ans = [];
const dfs = root => {
if (!root) {
return null;
}
root.left = dfs(root.left);
root.right = dfs(root.right);
if (!s[root.val]) {
return root;
}
if (root.left) {
ans.push(root.left);
}
if (root.right) {
ans.push(root.right);
}
return null;
};
if (dfs(root)) {
ans.push(root);
}
return ans;
};
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
export function delNodes(root: T, to_delete: number[]): Array<T> {
if (!root) return [];
const del = new Set(to_delete);
const res: T[] = [];
let q: TreeNode[] = [root];
while (q.length) {
const qNext: TreeNode[] = [];
for (const node of q) {
if (node.left) {
qNext.push(node.left);
if (del.has(node.left.val)) {
node.left = null;
}
}
if (node.right) {
qNext.push(node.right);
if (del.has(node.right.val)) {
node.right = null;
}
}
if (del.has(node.val)) {
if (node.left) res.push(node.left);
if (node.right) res.push(node.right);
}
}
q = qNext;
}
if (!del.has(root.val)) res.push(root);
return res;
}
type T = TreeNode | null;
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @param {number[]} to_delete
* @return {TreeNode[]}
*/
var delNodes = function (root, to_delete) {
if (!root) return [];
const del = new Set(to_delete);
const res = [];
let q = [root];
while (q.length) {
const qNext = [];
for (const node of q) {
if (node.left) {
qNext.push(node.left);
if (del.has(node.left.val)) {
node.left = null;
}
}
if (node.right) {
qNext.push(node.right);
if (del.has(node.right.val)) {
node.right = null;
}
}
if (del.has(node.val)) {
if (node.left) res.push(node.left);
if (node.right) res.push(node.right);
}
}
q = qNext;
}
if (!del.has(root.val)) res.push(root);
return res;
};