题目描述:

给出二叉树的根节点 root,树上每个节点都有一个不同的值。

如果节点值在 to_delete 中出现,我们就把该节点从树上删去,最后得到一个森林(一些不相交的树构成的集合)。

返回森林中的每棵树。你可以按任意顺序组织答案。

示例:

输入:root = [1,2,3,4,5,6,7], to_delete = [3,5]
输出:[[1,2,null,4],[6],[7]]

提示:

树中的节点数最大为 1000。
每个节点都有一个介于 1 到 1000 之间的值,且各不相同。
to_delete.length <= 1000
to_delete 包含一些从 1 到 1000、各不相同的值。

思路分析:

涉及树,利用递归求解。对于每一棵树,若其在待删除的结点数组中,且其父节点不在当前森林中,则添加这棵树到当前森林。其中在递归过程可以更新每棵树的左右子树,根据其左右子树的根结点是否在待删除结点数组中进行判断,若在,则当前的树的对应左右子树置为空。

代码:

 /**
* 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:
unordered_set<int> delete_node;
vector<TreeNode*>res;
TreeNode* preorder(TreeNode* root, bool n_root)
{
if(root == nullptr)
return nullptr;
bool is_delete = delete_node.count(root->val)>;
if(!is_delete && n_root)
res.push_back(root);
root->left = preorder(root->left, is_delete);
root->right = preorder(root->right, is_delete);
return is_delete ? nullptr:root;
}
vector<TreeNode*> delNodes(TreeNode* root, vector<int>& to_delete) {
if(root==nullptr)
return res;
delete_node = unordered_set<int>(to_delete.begin(), to_delete.end());
preorder(root, true);
return res;
}
};

leetcode 1110. 删点成林的更多相关文章

  1. LeetCode 1110. Delete Nodes And Return Forest

    原题链接在这里:https://leetcode.com/problems/delete-nodes-and-return-forest/ 题目: Given the root of a binary ...

  2. Leetcode题解 - DFS部分题目代码+思路(756、1034、1110、491、721、988)

    756. 金字塔转换矩阵 """ 学到的新知识: from collections import defaultditc可以帮我们初始化字典,不至于取到某个不存在的值的时 ...

  3. LeetCode刷题总结-树篇(下)

    本文讲解有关树的习题中子树问题和新概念定义问题,也是有关树习题的最后一篇总结.前两篇请参考: LeetCode刷题总结-树篇(上) LeetCode刷题总结-树篇(中) 本文共收录9道题,7道中等题, ...

  4. [leetcode]380. Insert Delete GetRandom O(1)设计数据结构,实现存,删,随机取的时间复杂度为O(1)

    题目: Design a data structure that supports all following operations in average O(1) time.1.insert(val ...

  5. 【LeetCode】1110. Delete Nodes And Return Forest 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  6. [leetcode]680. Valid Palindrome II有效回文II(可至多删一原字符)

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  7. 【leetcode】1110. Delete Nodes And Return Forest

    题目如下: Given the root of a binary tree, each node in the tree has a distinct value. After deleting al ...

  8. leetcode 学习心得 (2) (301~516)

    源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...

  9. [LeetCode] Total Hamming Distance 全部汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

随机推荐

  1. net core 记录自定义端口多个方式

    1.直接修改 . 2.代码定义 public class Program { public static void Main(string[] args) { CreateWebHostBuilder ...

  2. 初识AspNet Core中的标识Identity

    AspNet Core中的标识Identity,是用于Web应用程序的成员身份验证系统. 最方便的引入办法是在创建MVC或Pages的Web应用时,直接选择相应的身份验证系统.如图: 如果选择的是“个 ...

  3. Java自学-I/O 中文问题

    Java中的编码中文问题 步骤 1 : 编码概念 计算机存放数据只能存放数字,所有的字符都会被转换为不同的数字. 就像一个棋盘一样,不同的字,处于不同的位置,而不同的位置,有不同的数字编号. 有的棋盘 ...

  4. Python 字符串多替换时性能基准测试

    结论 先说结果, 直接替换是最好的. replace 一层层用, 方法笨了一点, 还可以. 时间消耗: tx2 < tx3 < tx1 < tx4 t2 < t3 < t ...

  5. javascript中常用函数

    1.js 获取文件后缀名 <script type="text/javascript"> var filename="www/data/index.php&q ...

  6. MUI下拉菜单样式

    <div class="mui-input-row my_select"> <label style="width: 47px;padding-righ ...

  7. 26.Apache Solr RCE

    多事之秋,刚爆出来shiro的RCE,紧接着solr服务器就出了RCE 自从漏洞爆出来已经一段时间,复现漏洞多如牛毛,我这里来水一篇 漏洞简介 什么是solr服务器? Solr是一个独立的企业级搜索应 ...

  8. SpringBoot自定义Condition注解

        最近碰到个这样的需求,需要同一套代码适配个版本数据库(数据库不同,且部分表的字段及关联关系可能会不同),即这套代码配置不同的数据库都能跑.项目采用的框架为SpringBoot+Mybatis. ...

  9. SQLServer2017自动备份数据库

    本文截图来源: 地址一:https://blog.csdn.net/u010944388/article/details/86674078 地址二:https://www.cnblogs.com/ja ...

  10. OpenSSL 安装 (Linux系统)

    OpenSSL 编译安装 操作系统:CentOS 7 OpenSSL Version: openssl-1.1.1d.tar.gz OpenSSL下载地址为:https://www.openssl.o ...