题目描述:

给出二叉树的根节点 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. ASP.NET MVC EF 连接数据库(二)-----Model First

    Model first (VS2015 ,Sql Server2014) 新建MVC项目     右键product ,新增标量属性(数据库表中的字段)   Ctrl + S 保存页面,右键“根据模型 ...

  2. 实现个虚拟机只要几百行的 toy 版就够了

    关键是 指令 的 eval 并把 高层代码进行翻译. 典型的项目: 1.  java-compiler (C++) 2. 手把手教你构建 C 语言编译器(0)- 前言 | 三点水  (C)

  3. c# 编码风格

    此内容为copy别人的,仅供自己参看.如有意见,麻烦通知我,谢谢 1. C# 代码风格要求 1.1注释 类型.属性.事件.方法.方法参数,根据需要添加注释. 如果类型.属性.事件.方法.方法参数的名称 ...

  4. android studio学习---标签页分离,满足查同一个文件的不同部分

    分离一个标签窗口:右键标签页,打开上下文菜单,选择Split Vertically or Split Horizontall改变分离窗口的摆放方式:右键标签页,打开上下文菜单,选择 Change Sp ...

  5. JavaWeb 发送邮件

    我们可以使用第三方的邮箱服务器来发送邮件. 常用的邮件传输协议有2种:POP3/SMTP.IMAP/SMTP. POP和IMAP的区别:在邮箱客户端的操作,比如移动邮件.标记已读,如果使用POP,是不 ...

  6. Centos7添加磁盘并分区格式化

    1.安装前准备 [root@localhost ~]# yum install xfsprogs [root@localhost ~]# modprobe xfs [root@localhost ~] ...

  7. intellij idea 新建springboot工程pom.xml报错

    今天使用idea新建的springboot工程pom.xml文件报错如下 1. 问题 'settings.xml' has syntax errors less... (Ctrl+F1) Inspec ...

  8. brew安装mongodb报错Error: No available formula with the name 'mongodb'

    原因:MongoDB不再是开源的了,并且已经从Homebrew中移除 #43770 设定:  $ brew tap mongodb/brew 安装: $ brew install mongodb-co ...

  9. Ubuntu 16.04.6 + Win10 双系统时间错误且不一致

    1.在Win系统下,按Win键 + R,输入regedit 运行, 2.打开注册表,按照路径查找     计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet ...

  10. 【Servlet】Servlet的配置

    创建时间:6.15 Servlet的配置 1. 基本配置 其中url-pattern的配置方式: 1)完全匹配 访问的资源与配置的资源完全相同才能访问到 2)目录匹配 格式:/虚拟的目录../*   ...